【问题标题】:How to force an HTTPS callback using Microsoft.AspNetCore.Authentication.Google?如何使用 Microsoft.AspNetCore.Authentication.Google 强制 HTTPS 回调?
【发布时间】:2016-11-04 07:35:32
【问题描述】:

我正在使用 Google 身份验证创建 AspNetCore 应用程序。我正在 Ubuntu 服务器上的 nginx 反向代理后面部署这个应用程序。 几乎一切正常,但回调 url 有问题。

在 Google 开发者控制台中,我将 http://localhost:5000/signin-google 设置为授权重定向 URI。这可以按预期工作,并允许我在从我的工作站运行时使用 Google 身份验证。

对于生产,我将https://myserver/signin-google 设置为授权的重定向 URI。但是,当我尝试使用它时,我从accounts.google.com 收到一个错误,指出http://myserver/signin-google(请注意缺少的s)未经授权。确实如此;它不应该被授权,我的服务器甚至不响应端口 80 请求。

如何告诉身份验证中间件我需要它使用 HTTPS 作为回调 URL?

【问题讨论】:

    标签: asp.net-mvc nginx asp.net-identity google-authentication


    【解决方案1】:

    我终于明白了。

    第 1 步:确保 Nginx 正在发送必要的转发标头,例如:

    server {
        # other stuff ...
        location / {
            # other stuff ...
            proxy_set_header X-Forwarded-Proto $scheme;
            # you could also just hardcode this to https if you only accept https
        }
    }
    

    第 2 步:默认情况下,AspNetCore 将忽略这些标头。安装处理它的中间件:

    PM> Install-Package Microsoft.AspNetCore.HttpOverrides
    

    第 3 步:在您的 Configure 函数中,应用中间件。

    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedProto
    });
    

    这应该正确地将Context.Request.Scheme 值更改为https,这将导致身份验证中间件生成正确的redirect_uri

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 2011-11-23
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2016-05-07
      • 2019-05-25
      相关资源
      最近更新 更多