Facebook 登录建议需要 HTTPS - 如何在 ASP.NET MVC 中为 Facebook 登录配置 HTTP 重定向 URL?

Facebook Login recommending to require HTTPS - How to Configure HTTP redirect URL for Facebook Login in ASP.NET MVC?

Facebook 建议我使用 HTTPS 重定向 URL,而不是 HTTP。我一直在尝试找到一种方法来配置它以生成 HTTPS URL,目前它正在生成 HTTP URL。

https://www.facebook.com/v2.8/dialog/oauth?response_type=code&client_id=255162614498922&redirect_uri=http://example.com/signin-facebook&scope=&state=-x4AVtFysadfadsfsadROH6E1QJ82gv4e4j48s32K5xbmqlF-JFbE5Y2Tx_MAdSquCP6CjZjic8Ye6gwasdfdfask3PXWkyxS42Ajpks9IuumDOl6CUJsadfafsasfdasdfbfpEFUDyxJUR3fARlWc83Lysadffdsdaffsdafasdsdafx_ziTnttz

目前它正在生成:http://example.com/signin-facebook 用于 redirect_uri,但我想要一个 HTTPS URL 将用户重定向到。

有没有办法将其配置为生成 HTTPS URL?

这与软件包 Microsoft.Owin.Security 和 Microsoft.Owin.Security.Facebook.

有关

目前我的 OwinStart 看起来像这样:

public class OwinStart
{
    public void Configuration(IAppBuilder app)
    {
            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Welcome")
            });

            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Configure Facebook authentication
            app.UseFacebookAuthentication(new FacebookAuthenticationOptions
            {
                AppId = ConfigurationManager.AppSettings["FacebookAppId"],
                AppSecret = ConfigurationManager.AppSettings["FacebookAppSecret"]
            });
    }
}

此外,在 FacebookAuthenticationOptions class 或 Challenge() 方法中似乎没有一种强制 HTTP 的方法来促使重定向到 Facebook:

internal class ChallengeResult : HttpUnauthorizedResult
{
    // TODO: Specify an XsrfKey?
    private const string XsrfKey = "SomethingHere";

    public ChallengeResult(string provider, string redirectUri)
        : this(provider, redirectUri, null)
    {
    }

    public ChallengeResult(string provider, string redirectUri, string userId)
    {
        this.LoginProvider = provider;
        this.RedirectUri = redirectUri;
        this.UserId = userId;
    }

    public string LoginProvider { get; set; }
    public string RedirectUri { get; set; }
    public string UserId { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        var properties = new AuthenticationProperties { RedirectUri = this.RedirectUri };

        if (this.UserId != null)
        {
            properties.Dictionary[XsrfKey] = this.UserId;
        }

        context.HttpContext.GetOwinContext().Authentication.Challenge(properties, this.LoginProvider);
    }
}

感谢 Microsoft 的 Chris Ross 的帮助,我 raising the issue on Github 得到了这个问题的答案。

Microsoft.Owin.Security Nuget 包似乎生成了 request_uri,它指示 Facebook 根据当前请求上下文使用。

就我而言,我 运行 我的所有服务器都通过 HTTP(而非 HTTPS),负载均衡器为我处理所有 HTTPS 内容。 IE。负载平衡器正在切断 SSL 连接。

确保包生成 HTTPS 的方法是在基于从负载平衡器转发的 x-forwarded-proto header 的 OwinStart 配置方法中使用中间件,如下所示:

app.Use((context, next) =>
{
  if (context.Request.Headers["x-forwarded-proto"] == "https")
  {
    context.Request.Scheme = "https";
  }
  return next();
});
// Use Cookies
// Use Facebook

所以我的 OwinStart 现在看起来像这样:

public class OwinStart
{
    public void Configuration(IAppBuilder app)
    {
        app.Use((context, next) =>
        {
            if (context.Request.Headers["x-forwarded-proto"] == "https")
            {
              context.Request.Scheme = "https";
            }
            return next();
        });

        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Welcome")
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Configure Facebook authentication
        app.UseFacebookAuthentication(new FacebookAuthenticationOptions
        {
            AppId = ConfigurationManager.AppSettings["FacebookAppId"],
            AppSecret = ConfigurationManager.AppSettings["FacebookAppSecret"]
        });
    }
}