AppHarbor 的应用程序位于 NGINX 负载平衡器之后。因此,所有到达客户端应用程序的请求都将通过 HTTP 传输,因为 SSL 将由该前端处理。
ASP.NET MVC 的 OAuth 2 OAuthAuthorizationServerOptions 有选项可以限制对 token 请求的访问只使用 HTTPS。问题是,与 Controller 或 ApiController 不同,当我指定 AllowInsecureHttp = false 时,我不知道如何允许这些转发的请求。
具体来说,在应用启动/配置中:
app.UseOAuthBearerTokens(new OAuthAuthorizationServerOptions {
AllowInsecureHttp = true,
});
需要以某种方式在内部进行此检查,如果为真,则将其视为 SSL:
HttpContext.Request.Headers["X-Forwarded-Proto"] == "https"
以下是我通过应用自定义过滤器属性使用 MVC Controller 的方法: https://gist.github.com/runesoerensen/915869
最佳答案
您可以尝试注册一些中间件,这些中间件可以根据 nginx 转发的 header 修改请求。您可能还想将远程 IP 地址设置为 X-Forwarded-For
header 的值。
像这样的东西应该可以工作(未经测试):
public class AppHarborMiddleware : OwinMiddleware
{
public AppHarborMiddleware(OwinMiddleware next)
: base(next)
{
}
public override Task Invoke(IOwinContext context)
{
if (string.Equals(context.Request.Headers["X-Forwarded-Proto"], "https", StringComparison.InvariantCultureIgnoreCase))
{
context.Request.Scheme = "https";
}
var forwardedForHeader = context.Request.Headers["X-Forwarded-For"];
if (!string.IsNullOrEmpty(forwardedForHeader))
{
context.Request.RemoteIpAddress = forwardedForHeader;
}
return Next.Invoke(context);
}
}
确保在配置身份验证中间件之前添加它:
app.Use<AppHarborMiddleware>();
app.UseOAuthBearerTokens(new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = false,
});
关于asp.net-mvc - AppHarbor 的反向代理导致 SSL 和 app.UseOAuthBearerTokens ASP.NET MVC 5 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24213614/