Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"CryptographicException: Cannot find the requested object" while the certificate file exists

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?

like image 451
disasterkid Avatar asked Sep 22 '15 11:09

disasterkid


1 Answers

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.

like image 145
Willy Van den Driessche Avatar answered Sep 21 '22 11:09

Willy Van den Driessche