【问题标题】:X509 certificate not loading private key file on serverX509 证书未在服务器上加载私钥文件
【发布时间】:2012-12-25 04:00:39
【问题描述】:

我正在使用 Google Analytics API,并按照这个 SO 问题设置 OAuth:https://stackoverflow.com/a/13013265/1299363

这是我的 OAuth 代码:

public void SetupOAuth ()
{
    var Cert = new X509Certificate2(
        PrivateKeyPath, 
        "notasecret", 
        X509KeyStorageFlags.Exportable);
    var Provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, Cert)
    {
        ServiceAccountId = ServiceAccountUser,
        Scope = ApiUrl + "analytics.readonly"
    };
    var Auth = new OAuth2Authenticator<AssertionFlowClient>(Provider, AssertionFlowClient.GetState);
    Service = new AnalyticsService(Auth);
}

PrivateKeyPath 是 Google API Console 提供的私钥文件的路径。这在我的本地机器上完美运行,但是当我将它推送到我们的测试服务器时,我得到了

System.Security.Cryptography.CryptographicException: An internal error occurred.

具有以下堆栈跟踪(已删除不相关部分):

System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +33
System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags) +237
System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags) +140
Metrics.APIs.GoogleAnalytics.SetupOAuth() in <removed>\Metrics\APIs\GoogleAnalytics.cs:36
Metrics.APIs.GoogleAnalytics..ctor(String PrivateKeyPath) in <removed>\Metrics\APIs\GoogleAnalytics.cs:31

所以它看起来好像在加载文件时遇到了问题。我检查了传入的 PrivateKeyPath,它指向正确的位置。

有什么想法吗?不知道是服务器、文件、代码的问题还是什么。

【问题讨论】:

    标签: c# asp.net-mvc-3 google-analytics-api x509certificate2


    【解决方案1】:

    我想到的一件事是您的应用程序池的身份,请确保加载用户配置文件已打开,否则加密子系统无法工作。

    【讨论】:

    • 这是加密 api 和应用程序池的常见问题,我也是其中之一。
    • 天哪。向您致敬,先生。非常感谢!
    • 关于如何在 azure WebApp 中设置此属性的任何想法?我需要加载证书,但无法解决
    • 哇,这是一个非常简单的解决方案,解决了不应该成为问题的问题。不错的答案!
    • @JReam 是的,我们管理加载证书,诀窍在构造函数中,必须使用 new X509Certificate2(cert, "MyAwesomePassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
    【解决方案2】:

    我正在加载我的 p12 文件

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

    即使File.Exists(filename) 返回 true,我实际上也得到了 FileNotFoundException。

    正如@Wiktor Zychla 所说,它就像启用Load User Profile 一样简单

    这是需要更改的设置图片

    在 IIS 的“应用程序池”下的应用程序池上右击,选择“高级设置”,你需要的设置大约是一半。

    提示:我建议使用此注释您的代码,以防止将来浪费时间,因为如果您以前从未遇到过它,它会非常晦涩。

      // If this gives FileNotFoundException see 
      // http://stackoverflow.com/questions/14263457/
    

    【讨论】:

    • 您好!我有同样的问题,我想,我在哪里做这些改变?谢谢
    • @KristofferAndersson 我已经更新了答案。只需右键单击 IIS 中的应用程序池并选择“高级设置”-然后投票给我 :-)
    • 嗯,看来我的问题一定是别的。所有的池都被破解为“真”。很奇怪.. 我的 Keypath 看起来像这样: var certificate = new X509Certificate2(@"Models/GAStatistics/\key.p12", "notasecret", X509KeyStorageFlags.Exportable);即使我将它放在我的 MVC 应用程序中,应用程序也无法本地化文件 (key.p12)。谢谢无论如何
    • 除了确保您查看的是正确的池之外,您还尝试在文件上使用File.Exists(...) 以确保它返回 true?
    • 不,“File.Exists(...)”是否也在高级设置中?我有 3 个池,它们都为“加载用户配置文件”启用了 true。我认为我的问题可能与依赖项和 NuGet 包有关,因为代码作为控制台应用程序运行良好,但在 MVC 中给我带来了问题。谢谢
    【解决方案3】:

    也可以尝试指定 X509KeyStorageFlags

        var certificate = new X509Certificate2(KeyFilePath, KeyFilePassword, 
    X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | 
    X509KeyStorageFlags.Exportable);
    

    【讨论】:

    • 添加 X509KeyStorageFlags.MachineKeySet,特别适合我。
    • 这是一个很好的解决方案,可以避免弄乱 IIS 的配置
    • 如果设置 X509KeyStorageFlags 参数,每次实例化 X509Certificate2 对象时,它都会在 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 文件夹中创建一个密钥文件,如果你经常这样做,你可以结束已填满磁盘,因此请谨慎使用。
    • 它希望一个文件位于 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys - 如果您不将 MachineKeySet 放在选项中,则不会创建该文件。在 PowerShell 中看起来像这样:New-Object System.Security.Cryptography.X509Certificates.X509Certificate2.Import($certPath,$certPass,"MachineKeySet,Exportable,PersistKeySet")
    【解决方案4】:

    如上所述,您需要配置 IIS,但在我们的情况下,有时您需要检查以下文件夹的权限:

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

    如果您设置X509KeyStorageFlags 参数,它将在此文件夹中创建一个密钥文件。就我而言,此文件夹的权限有所不同。池帐户未添加到上述文件夹中。

    【讨论】:

    • 但是,授予的权限是什么?该权限授予哪个用户?
    • 如前所述,需要添加应用程序池帐户。
    【解决方案5】:

    不,“File.Exists(...)”是否也在高级设置中?我有 3 个池,它们都为“加载用户配置文件”启用了 true。我认为我的问题可能与依赖项和 NuGet 包有关,因为代码作为控制台应用程序运行良好,但在 MVC 中给我带来了问题。

    【讨论】:

    • 我的情况一模一样,请问您是怎么​​解决问题的?
    • 我不明白这是如何回答这个问题的。
    猜你喜欢
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2017-11-18
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    相关资源
    最近更新 更多