我正在复制社会样本 https://github.com/aspnet/Security/tree/1.0.0-beta4/samples/SocialSample但尝试使用此处未显示的不同 OAuth 提供程序,所以我有一些如下代码:
app.UseOAuthAuthentication("test", options =>
{
options.ClientId = "xxx";
options.ClientSecret = "xxx";
options.AuthorizationEndpoint = "xxx";
options.TokenEndpoint = "xxx";
options.CallbackPath = new PathString("/signin-test");
});
我的应用程序在终止 SSL 的反向代理后面运行。我的redirect_uri 然后变成类似的东西http://myapp.com/signin-test http://myapp.com/signin-test当我需要它的时候https://myapp.com/signin-test https://myapp.com/signin-test.
应用程序只知道 X-Forwarded-Proto HTTP 标头是否设置为“http”或“https”,但似乎没有检测到。
有没有办法强制 CallbackPath 使用 HTTPS,无论请求是否使用?或者还有其他方法可以实现这一点?
我尝试将所有请求重定向到 HTTPS,但这没有帮助:
app.Use(async (context, next) =>
{
if ("https".Equals(context.Request.Headers["x-forwarded-proto"]))
{
await next();
}
else
{
var withHttps = "https://" + context.Request.Host + context.Request.Path;
context.Response.Redirect(withHttps);
}
});
推荐的方法是手动更改context.Request.Scheme
回来https
代替http
在调用管道的其余部分之前(await next()
):
app.Use(next => context => {
if (string.Equals(context.Request.Headers["X-Forwarded-Proto"], "https", StringComparison.OrdinalIgnoreCase)) {
context.Request.Scheme = "https";
}
return next(context);
});
仅供参考,ASP.NET 团队目前正在开发一个可以自动执行此任务的中间件:https://github.com/aspnet/BasicMiddleware/pull/1/files https://github.com/aspnet/BasicMiddleware/pull/1/files
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)