简体   繁体   中英

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.

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.

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