【问题标题】:Error configuring ASP.NET Core web application with Azure App service and Facebook identity使用 Azure 应用服务和 Facebook 身份配置 ASP.NET Core Web 应用程序时出错
【发布时间】:2019-06-04 02:43:35
【问题描述】:

我的 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://error。但是,我已经在 facebook 配置=>“有效 OAuth 重定向 URI”下设置了我的应用程序的安全 url(请参见下面的屏幕截图)。

我错过了什么?

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();
    }

基于this链接的故障排除输出:

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:

【问题讨论】:

  • 听起来您的应用在构建登录 URL 时生成了错误的 redirect_uri。因此,您的设置中的某些内容可能仍然认为该应用正在 HTTP 上运行。

标签: c# facebook azure asp.net-core asp.net-identity


【解决方案1】:

对我有用的是记录在案的解决方案 here 。还需要在 Microsoft Azure 中添加 ASPNETCORE_FORWARDEDHEADERS_ENABLED=true 应用程序设置。

// 配置服务

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();
                });
            }

【讨论】:

    【解决方案2】:

    在 facebook 开发者中,您已经使用 appId 和 appSecret 注册了一个应用程序,并且您在本地对其进行了很好的测试。将网站发布到 Azure Web 应用后,您应该在 Facebook 开发人员门户中重置 AppSecret

    根据您的描述,您在 FaceBook API OAUTH 屏幕中设置的 OAUTH URL 似乎与您尝试用作 OAUTH 的 URL 不匹配。

    将以下 URL 添加到 Valid OAuth redirect URIs 字段:https://yourwebsite.azurewebsites.net/auth/facebook/callback 其中 yoursite.com 是您的域。并将 ssl 证书添加到您的 azure 网站。

    【讨论】:

    • 根据 Microsoft 文档,我正在使用带有 https 协议的 url simplerproductsscrubber.azurewebsites.net/signin-facebook。一个类似的网址正在我的本地盒子上工作。我将尝试在 Facebook 中重置我的 appsecret,看看是否有助于解决问题。
    • 正如我所怀疑的,在 Facebook 开发者门户中重置 AppSecret 并不能解决问题。 redirect_uri 使用“http”协议而不是“https”协议显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2012-07-27
    • 2021-10-28
    • 1970-01-01
    • 2018-05-19
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多