X509证书未在服务器上加载私钥文件

acu*_*cie 41 c# x509certificate2 google-analytics-api asp.net-mvc-3

我正在使用Google AnalyticsAPI,我按照这个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);
}
Run Code Online (Sandbox Code Playgroud)

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

System.Security.Cryptography.CryptographicException: An internal error occurred.
Run Code Online (Sandbox Code Playgroud)

使用以下堆栈跟踪(删除不相关的部分):

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
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?我不知道这是服务器,文件,代码还是什么的问题.

Wik*_*hla 83

我想到的一件事是应用程序池的标识,确保加载用户配置文件已打开,否则加密子系统不起作用.

  • 就是这样!我不知道你怎么做但是谢谢你! (3认同)
  • 这是加密api和应用程序池的常见问题,我也是受到这种打击的人之一. (2认同)
  • @JReam是的,我们管理加载证书,诀窍是在构造函数中,必须使用新的X509Certificate2(cert,"MyAwesomePassword",X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); (2认同)

Sim*_*ver 30

我正在加载我的p12文件

new X509Certificate2(
HostingEnvironment.MapPath(@"~/App_Data/GoogleAnalytics-privatekey.p12"), ....
Run Code Online (Sandbox Code Playgroud)

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

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

这是需要更改的设置的图像

只需右键单击IIS中"应用程序池"下的应用程序池,然后选择"高级设置",您需要的设置大约是一半.

在此输入图像描述

提示:我建议您使用此代码对代码进行评论,以防止将来浪费时间,因为如果您以前从未遇到过它,那么它就会变得如此模糊.

  // If this gives FileNotFoundException see 
  // http://stackoverflow.com/questions/14263457/
Run Code Online (Sandbox Code Playgroud)


Vah*_*idN 11

还可以尝试指定X509KeyStorageFlags

    var certificate = new X509Certificate2(KeyFilePath, KeyFilePassword, 
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | 
X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)

  • 特别添加 X509KeyStorageFlags.MachineKeySet 对我有用。 (2认同)
  • 如果您设置X509KeyStorageFlags参数,它将在每次实例化X509Certificate2对象时在C:\ ProgramData\Microsoft\Crypto\RSA\MachineKeys文件夹中创建一个密钥文件,如果您经常这样做,最终可能会填满磁盘,因此请使用这个仔细. (2认同)