c# - X509Certificate2 Import PathTooLongException : The specified path, 文件名,或两者都太长

标签 c# x509certificate2

我正在尝试从 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/

相关文章:

c# - 在 C# 中将 Xml 反序列化为对象

c# - Xamarin Form 在空字符串的搜索栏中触发搜索命令

c# - 使用 Microsoft Graph token 通过 Jwt Bearer token 保护 ASP.NET Core Web API

c# - Google API - Godaddy 上的服务帐户 C#

c# - X509 证书未在服务器上加载私钥文件

c# - .Net 框架更新后 EncryptedXml DecryptDocument 方法错误

c# - 如何在 C# 中修复 SQLite 查询?

c# - Linq 语句中的 TSource 有什么意义?

c# - 为什么服务器可能收不到附加到使用 HttpClient 的请求的证书?

c# - 在受信任的发布者商店中找不到证书