7

我想在我的 MVC 网站中使用谷歌分析 api,我使用 api 服务帐户和 oauth2 进行身份验证,在我的本地主机上没有问题,但是一旦我部署到 Azure,我就会收到 502 错误:

“502 - Web 服务器在充当网关或代理服务器时收到无效响应。您要查找的页面有问题,无法显示。当 Web 服务器(充当网关或代理服务器)联系时上游内容服务器,它收到了来自内容服务器的无效响应。”

这是我的代码:

const string ServiceAccountUser = "xxxxxxxxxx-cpla4j8focrebami0l87mbcto09j9j6k@developer.gserviceaccount.com";
AssertionFlowClient client = new AssertionFlowClient(
        GoogleAuthenticationServer.Description,
            new X509Certificate2(System.Web.Hosting.HostingEnvironment.MapPath("/Areas/Admin/xxxxxxxxxxxxxxxxxx-privatekey.p12"), 
                "notasecret", X509KeyStorageFlags.Exportable))
        {
            Scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue(),
            ServiceAccountId = ServiceAccountUser //Bug, why does ServiceAccountUser have to be assigned to ServiceAccountId
            //,ServiceAccountUser = ServiceAccountUser
        };
        OAuth2Authenticator<AssertionFlowClient> authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

我想不通是什么原因造成的?我在 Azure 中遗漏了什么吗?

谢谢你的帮助。

4

2 回答 2

16

我也遇到了同样的问题,但传递X509KeyStorageFlags.MachineKeySet给构造函数也为我解决了这个问题。

X509Certificate2 certificate = new X509Certificate2(file, "key", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
于 2013-11-18T10:16:56.730 回答
9

在对这个完全相同的问题进行了数小时的痛苦之后,我通过拼凑各种信息来源找到了解决方法。

问题源于尝试从 Azure 网站读取 p12 文件,即我的代码中的这一行失败

var key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);

不知道为什么,但是如果您将文件拆分为 cer 和 key.xml 文件,它会起作用吗?

首先,提取这些文件,(我只是使用了一个控制台应用程序)

// load pfx/p12 as "exportable"
var p12Cert = new X509Certificate2(@"c:\Temp\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable);

// export .cer from .pfx/.p12
File.WriteAllBytes(@"C:\Temp\MyCert.cer", p12Cert.Export(X509ContentType.Cert));

// export private key XML
string privateKeyXml = p12Cert.PrivateKey.ToXmlString(true);

File.WriteAllText(@"C:\Temp\PrivateKey.xml", privateKeyXml);

然后将它们复制到您的网站,然后像这样加载它们

//Store the authentication description
AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

//Create a certificate object to use when authenticating

var rsaCryptoServiceProvider = new RSACryptoServiceProvider();
rsaCryptoServiceProvider.FromXmlString(File.ReadAllText(keyFile));
var key = new X509Certificate2(certFile) {PrivateKey = rsaCryptoServiceProvider};


//Now, we will log in and authenticate, passing in the description
//and key from above, then setting the accountId and scope
var client = new AssertionFlowClient(desc, key)
{
    //cliendId is your SERVICE ACCOUNT Email Address from Google APIs Console
    //looks something like 12345-randomstring@developer.gserviceaccount.com
    //~IMPORTANT~: this email address has to be added to your Google Analytics profile
    // and given Read & Analyze permissions
    ServiceAccountId = clientId,
    Scope = "https://www.googleapis.com/auth/analytics.readonly"
};

//Finally, complete the authentication process
//NOTE: This is the first change from the update above
var auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

//First, create a new service object
//NOTE: this is the second change from the update
//above. Thanks to James for pointing this out
var gas = new AnalyticsService(new BaseClientService.Initializer { Authenticator = auth });

这现在对我有用,我希望它对你有帮助。

于 2013-06-10T04:23:54.223 回答