简体   繁体   中英

X509Certificate2 Import PathTooLongException: The specified path, file name, or both are too long

I am trying to import a X509Certificate2 from a base64string and getting exception "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters". Can you kindly help as to what this exception means.

 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 is for loading your certificate from a file path. Since your base64 representation is far too long to be a path, it's throwing the exception you are getting.

Instead, you could use this overload method , which takes in the raw data as a byte array.

var pfx = certficatestring;          

var bytes = Encoding.UTF32.GetBytes(pfx);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM