【问题标题】:X509Certificate2 Import PathTooLongException: The specified path, file name, or both are too longX509Certificate2 Import PathTooLongException:指定的路径、文件名或两者都太长
【发布时间】:2018-06-07 01:31:27
【问题描述】:

我正在尝试从 base64string 导入 X509Certificate2 并收到异常“指定的路径、文件名或两者都太长。完全限定的文件名必须小于 260 个字符,并且目录名必须小于248 个字符”。你能帮忙解释一下这个例外是什么意思吗?

 var pfx = certficatestring;          

 var bytes = Encoding.UTF32.GetBytes(pfx);
 var certdata = Convert.ToBase64String(bytes);

 X509Certificate2 x509 = new X509Certificate2();
 x509.Import(certdata,password,X509KeyStorageFlags.Exportable);
 return x509;

【问题讨论】:

    标签: c# x509certificate2


    【解决方案1】:

    The overload you are using 用于从文件路径加载您的证书。由于您的 base64 表示形式太长而不能成为路径,因此它会抛出您得到的异常。

    相反,您可以使用this overload method,它将原始数据作为字节数组接收。

    var pfx = certficatestring;          
    
    var bytes = Encoding.UTF32.GetBytes(pfx);
    
    X509Certificate2 x509 = new X509Certificate2();
    x509.Import(bytes,password,X509KeyStorageFlags.Exportable);
    return x509;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多