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

前端之家收集整理的这篇文章主要介绍了c# – Web服务器在充当网关或代理服务器时收到无效响应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试使用具有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时,它会返回错误.

解决方法

我花了大约2个小时来追查问题

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

通过更改以下行

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

对此

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

Web应用程序和服务帐户现在可以在Azure上运行.我希望这可以帮助将来的某个人.

原文链接:https://www.f2er.com/csharp/239048.html

猜你在找的C#相关文章