在带有 kestrel 的反向代理服务器中 Google API 中的 HTTPS 请求 URI
Request URI in HTTPS in Google API in reverse proxy server with kestrel
背景:
在 Apache 后面使用带有 ASP.NET 核心的 Kestrel 作为反向代理。对本网站的所有流量使用 https 协议。
我的问题是,如何使用 Google API 在回调 uri 中强制使用 https 而不是 http 进行响应?
我在调用 Google API 后收到一条错误消息。错误消息是 "Error: redirect_uri_mismatch"。我已经在 Google API 控制台以 https 作为协议正确设置了回调。
请求详情如下:
> response_type=code
client_id=myclientidnum.apps.googleusercontent.com
redirect_uri=http://mysub.domain.com/signin-google
scope=openid profile email
state=long_long_code
如果您有任何想法,请给我留言。
当我尝试通过 Kestrel 的反向代理在 Apache 上为我的 ASP.NET 核心 Web 应用程序 运行 配置 Google 身份验证时,我也收到了 redirect_uri_mismatch 错误。
错误是由于将授权重定向 URI 设置为 https
。一旦我将其更改为 http
,Google 身份验证就起作用了。
因此,假设您的域是 example.com
,将您的授权重定向 URI 从
更改为
https://example.com/signin-google
到
http://example.com/signin-google
问题是服务器没有使用由反向代理转发的 headers,因此请求的协议不正确。您需要:
- 确保您的反向代理正在设置 X-Forward headers
- 启用 UseForwardedHeaders 中间件在 认证之前
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders =
ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
我遇到了同样的问题,并通过以下代码解决了这个问题。
app.Use(async (ctx, next) =>
{
ctx.Request.Scheme = "https";
await next();
});
ForwardedHeadersOptions forwardOptions = new
ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto,
RequireHeaderSymmetry = false
};
forwardOptions.KnownNetworks.Clear();
forwardOptions.KnownProxies.Clear();
app.UseForwardedHeaders(forwardOptions);
现在 google 和 facebook 不支持生产应用程序中的 http 重定向,反向代理后面的 kestrel 在 http 上 运行 也面临同样的问题。我使用上面的代码强制对 https 的所有请求进行架构(这是我在 https 反向代理后面的安全赌注)并且一切正常
背景: 在 Apache 后面使用带有 ASP.NET 核心的 Kestrel 作为反向代理。对本网站的所有流量使用 https 协议。
我的问题是,如何使用 Google API 在回调 uri 中强制使用 https 而不是 http 进行响应?
我在调用 Google API 后收到一条错误消息。错误消息是 "Error: redirect_uri_mismatch"。我已经在 Google API 控制台以 https 作为协议正确设置了回调。
请求详情如下:
> response_type=code
client_id=myclientidnum.apps.googleusercontent.com
redirect_uri=http://mysub.domain.com/signin-google
scope=openid profile email
state=long_long_code
如果您有任何想法,请给我留言。
当我尝试通过 Kestrel 的反向代理在 Apache 上为我的 ASP.NET 核心 Web 应用程序 运行 配置 Google 身份验证时,我也收到了 redirect_uri_mismatch 错误。
错误是由于将授权重定向 URI 设置为 https
。一旦我将其更改为 http
,Google 身份验证就起作用了。
因此,假设您的域是 example.com
,将您的授权重定向 URI 从
https://example.com/signin-google
到
http://example.com/signin-google
问题是服务器没有使用由反向代理转发的 headers,因此请求的协议不正确。您需要:
- 确保您的反向代理正在设置 X-Forward headers
- 启用 UseForwardedHeaders 中间件在 认证之前
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders =
ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
我遇到了同样的问题,并通过以下代码解决了这个问题。
app.Use(async (ctx, next) =>
{
ctx.Request.Scheme = "https";
await next();
});
ForwardedHeadersOptions forwardOptions = new
ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto,
RequireHeaderSymmetry = false
};
forwardOptions.KnownNetworks.Clear();
forwardOptions.KnownProxies.Clear();
app.UseForwardedHeaders(forwardOptions);
现在 google 和 facebook 不支持生产应用程序中的 http 重定向,反向代理后面的 kestrel 在 http 上 运行 也面临同样的问题。我使用上面的代码强制对 https 的所有请求进行架构(这是我在 https 反向代理后面的安全赌注)并且一切正常