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
我想到的一件事是应用程序池的标识,确保加载用户配置文件已打开,否则加密子系统不起作用.
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)
| 归档时间: |
|
| 查看次数: |
25088 次 |
| 最近记录: |