使用Azure App服务和Facebook身份配置ASP.NET Core Web应用程序时出错

问题描述:

我的ASP.Net Core 2.2应用程序的"Facebook外部登录设置"正在我的本地计算机上运行,​​但不适用于Azure应用程序服务.当应用程序重定向到Facebook时,出现You can't get an access token or log in to this app from an insecure page. Try re-loading the page as https://错误.但是,我已经在facebook configuration =>有效的OAuth重定向URI"下设置了应用程序的安全网址(请参见下面的屏幕截图).

My ASP.Net Core 2.2 application's "Facebook external login setup" is working on my local machine but not working on Azure app service. I get a You can't get an access token or log in to this app from an insecure page. Try re-loading the page as https://error when the application redirects to facebook. However I have set up my application's secure url under facebook configuration=> "Valid OAuth Redirect URIs"(please see screenshot below).

我想念什么?

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
  services.Configure<ForwardedHeadersOptions>(options =>
  {
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
  });
.............

}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      app.UseForwardedHeaders();
    }

根据

Troubleshooting output based on this link:

Header: "X-Client-IP": ["76.187.198.247"]
Header: "X-Client-Port": ["51335"]
Header: "Upgrade-Insecure-Requests": ["1"]
Header: "DNT": ["1"]
Header: "X-WAWS-Unencoded-URL": ["/Identity/Account/Login"]
Header: "CLIENT-IP": ["76.187.198.247:51335"]
Header: "X-ARR-LOG-ID": ["3b69d760-03e7-4199-bec4-38ff77055413"]
Header: "DISGUISED-HOST": ["simplerproductsscrubber.azurewebsites.net"]
Header: "X-SITE-DEPLOYMENT-ID": ["SimplerProductsScrubber"]
Header: "WAS-DEFAULT-HOSTNAME": ["simplerproductsscrubber.azurewebsites.net"]
Header: "X-Original-URL": ["/Identity/Account/Login"]
Header: "X-Forwarded-For": ["76.187.198.247:51335"]
Header: "X-ARR-SSL": ["2048|256|C=US, O=DigiCert Inc, CN=DigiCert SHA2 Secure Server CA|C=US, S=Washington, L=Redmond, O=Microsoft Corporation, CN=*.azurewebsites.net"]
Header: "X-Forwarded-Proto": ["https"]
Header: "X-AppService-Proto": ["https"]
Request RemoteIp: "::ffff:172.16.1.1"
Request Method: "GET"
Request Scheme: "http"
Request Path: "/Identity/Account/Login"
Header: "Connection": ["close"]
Header: "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"]
Header: "Accept-Encoding": ["gzip, deflate, br"]
Header: "Accept-Language": ["en-US,en;q=0.9"]
Header: "Cookie": ["ARRAffinity=152c130e21c95ce31be52418aed58ed4a1114b560e108246b2120e2d4dbf27ee; .AspNetCore.Antiforgery.nixphHDAMN4=CfDJ8G1Jn3njIA5IoKC-W8RHjabWwnkwCrPq4ZnU7-ZRlTXbuf8kfpKPQACS5HEylcqol59j-9GJ4AzKFgirMIn8yclO5QSucBnlED9aKjQgAlRrkuZmIZeu8VKT9oOA1V_dvEpjhDoqKxxWrRpfVwST6hU"]
Header: "Host": ["simplerproductsscrubber.azurewebsites.net"]
Header: "Max-Forwards": ["10"]
Header: "Referer": ["https://simplerproductsscrubber.azurewebsites.net/Dashboard"]
Header: "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"]

Facebook配置:有效的OAuth重定向URI,显示我的网站配置了https:

对我有用的是记录在文档中的解决方案

What worked for me was the solution documented here . An ASPNETCORE_FORWARDEDHEADERS_ENABLED=true app setting also needs to be added in microsoft Azure.

//ConfigureServices

// ConfigureServices

if (string.Equals("true", hostingContext.Configuration["ForwardedHeaders_Enabled"], StringComparison.OrdinalIgnoreCase))
            {
                services.Configure<ForwardedHeadersOptions>(options =>
                {
                    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
                    // Only loopback proxies are allowed by default. Clear that restriction because forwarders are
                    // being enabled by explicit configuration.
                    options.KnownNetworks.Clear();
                    options.KnownProxies.Clear();
                });
            }