我正在尝试从 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;
最佳答案
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;
关于c# - X509Certificate2 Import PathTooLongException : The specified path, 文件名,或两者都太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731780/