X509 certificate not loading private key file on server

前端 未结 5 2085
忘了有多久
忘了有多久 2020-12-12 23:54

I\'m using the Google Analytics API and I followed this SO question to set up the OAuth: https://stackoverflow.com/a/13013265/1299363

Here is my OAuth code:

相关标签:
5条回答
  • 2020-12-13 00:32

    Nope, is "File.Exists(...)" also in advanced settings? I had 3 pools, all of them had true enabled for "Load User Profile". I'm thinking my problem might have something to do with dependencies and NuGet Packages as the code worked just fine as a Console App but gives me problem in MVC.

    0 讨论(0)
  • 2020-12-13 00:36

    I'm loading my p12 file with

    new X509Certificate2(
    HostingEnvironment.MapPath(@"~/App_Data/GoogleAnalytics-privatekey.p12"), ....
    

    I actually got a FileNotFoundException even though File.Exists(filename) returned true.

    As @Wiktor Zychla said it's as simple as enabling Load User Profile

    Here's an image of the setting that needs changing

    Just right click on the app pool under 'Application Pools' in IIS and select 'Advanced Settings' and the setting you need is about halfway down.

    enter image description here

    Tip: I'd recommend commenting your code with this to prevent future time wasted since it's so obscure if you've never come across it before.

      // If this gives FileNotFoundException see 
      // http://stackoverflow.com/questions/14263457/
    
    0 讨论(0)
  • 2020-12-13 00:37

    One of things that comes to my mind is the identity of your app pool, make sure that the Load user profile is turned on otherwise the crypto subsystem does not work.

    0 讨论(0)
  • 2020-12-13 00:52

    Also try specifying X509KeyStorageFlags

        var certificate = new X509Certificate2(KeyFilePath, KeyFilePassword, 
    X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | 
    X509KeyStorageFlags.Exportable);
    
    0 讨论(0)
  • 2020-12-13 00:56

    As mentioned above you need to configure IIS but as our case, some time you need to check the permission of the following folder:

    C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys

    If you set X509KeyStorageFlags parameter it will create a key file in this folder. In my case there was a difference in permission of this folder. Pool account was not added in the mentioned folder.

    0 讨论(0)
提交回复
热议问题