如何使用 Microsoft.AspNetCore.Authentication.Google 强制 HTTPS 回调?
How to force an HTTPS callback using Microsoft.AspNetCore.Authentication.Google?
我正在创建一个带有 Google 身份验证的 AspNetCore 应用程序。我正在 Ubuntu 服务器上的 nginx 反向代理后面部署此应用程序。 几乎一切正常,但我在处理回调时遇到问题url。
在 Google 开发人员控制台中,我将 http://localhost:5000/signin-google 设置为授权重定向 URI。这按预期工作,并允许我在 运行 来自我的工作站时使用 Google 身份验证。
对于生产,我 https://myserver/signin-google set as an authorized redirect URI. However, when I try to use it, I get an error from accounts.google.com that http://myserver/signin-google(注意缺少的 s)未获得授权。这是真的;它不应该被授权,我的服务器甚至不响应端口 80 请求。
如何告诉身份验证中间件我需要它使用 HTTPS 进行回调 URL?
我终于明白了。
第 1 步:确保 Nginx 正在发送必要的转发 headers,例如:
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 将忽略这些 headers。安装处理它的中间件:
PM> Install-Package Microsoft.AspNetCore.HttpOverrides
第 3 步:在您的 Configure
函数中,应用中间件。
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
这应该正确地将 Context.Request.Scheme
值更改为 https,这将导致身份验证中间件生成正确的 redirect_uri
。
我正在创建一个带有 Google 身份验证的 AspNetCore 应用程序。我正在 Ubuntu 服务器上的 nginx 反向代理后面部署此应用程序。 几乎一切正常,但我在处理回调时遇到问题url。
在 Google 开发人员控制台中,我将 http://localhost:5000/signin-google 设置为授权重定向 URI。这按预期工作,并允许我在 运行 来自我的工作站时使用 Google 身份验证。
对于生产,我 https://myserver/signin-google set as an authorized redirect URI. However, when I try to use it, I get an error from accounts.google.com that http://myserver/signin-google(注意缺少的 s)未获得授权。这是真的;它不应该被授权,我的服务器甚至不响应端口 80 请求。
如何告诉身份验证中间件我需要它使用 HTTPS 进行回调 URL?
我终于明白了。
第 1 步:确保 Nginx 正在发送必要的转发 headers,例如:
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 将忽略这些 headers。安装处理它的中间件:
PM> Install-Package Microsoft.AspNetCore.HttpOverrides
第 3 步:在您的 Configure
函数中,应用中间件。
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
这应该正确地将 Context.Request.Scheme
值更改为 https,这将导致身份验证中间件生成正确的 redirect_uri
。