Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: 'The system cannot find the file specified.' when trying to access certificate

I am using the Google API for BigQuery and it requires that I use a .p12 certificate. The issue is that no matter how I try to import my certificate resource I keep getting the same error:

The system cannot find the file specified.

I've added the file as follows:

var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

I'm new to C# and Visual Studio.

like image 492
Fullmetal_Alchemist_Fan Avatar asked Nov 19 '25 22:11

Fullmetal_Alchemist_Fan


1 Answers

in the properties window (select the p12 file and press f4), ensure the 'Copy to Output Directory' is set to 'Copy Always'. this will ensure the file gets copied to the ultimate EXE location.

basically when you say

new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

the code is expecting the key.p12 file right where the EXE (or DLL) is running from. it has lessto do with the location of the file in solution explorer, and more to do with the runtime location of the file.

also, can you try this snippet:

var certificate = new X509Certificate2(@"key.p12", "notasecret", 
                                         X509KeyStorageFlags.MachineKeySet);

this is because the user cert store is not loaded by asp.net.. the machine store is.

like image 89
Raja Nadar Avatar answered Nov 22 '25 11:11

Raja Nadar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!