I have a .p12
certificate file and I create my certificate like this:
var certificate = new X509Certificate2(certFileLocation, "mySecret", X509KeyStorageFlags.Exportable);
When certFileLocation
is on my desktop and I give the absolute path, the code works. But when I put the entire content of the .p12 file in a new file in my solution and set the Copy to Output Directory
property of the file to "Copy if newer" I get a CryptographicException
exception that says:
Cannot find the requested object
I also check every time whether the file is in place and it is. What is the difference between these two scenarios and why can't I read the file with the latter approach?
I had a similar problem. It worked with a fixed file but did not work with the file relative to my unit tests. This was so mindnumbling that I finally had a look at the files and compared them binary. They where not the same. They were read and written as strings, which caused them to be slightly different due to unicode interpretations. When I copied them myself (from a resourcestream) as binary (byte[]), everything worked again. I hope this solves your problem too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With