Web 服务器在充当网关或代理服务器时收到无效响应

Web server received an invalid response while acting as a gateway or proxy server

当尝试使用带有 Google .net 客户端库的服务帐户将 Web 应用程序部署到 Azure 时,它​​返回以下错误

502 - Web server received an invalid response while acting as a gateway or proxy server.

代码示例:

var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail) {
                        Scopes =  new string[] { AnalyticsService.Scope.Analytics };
                    }.FromCertificate(certificate));

// Create the service.
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Analytics API Sample",
                });

以上代码在 运行 开发中有效。但是,当部署到 AZURE 时,它 returns 出错了。

我花了大约 2 个小时才找到问题

问题与 Azure 处理证书的方式有关。

通过更改以下行

var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable);

至此

var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

Web 应用程序和服务帐户现在可以在 Azure 上运行。我希望这对以后的人有所帮助。