5

AppHarbor 的应用程序位于 NGINX 负载均衡器后面。因此,所有命中客户端应用程序的请求都将通过 HTTP 进行,因为 SSL 将由该前端处理。

ASP.NET MVC 的 OAuth 2 OAuthAuthorizationServerOptions 具有限制对令牌请求的访问以仅使用 HTTPS 的选项。问题是,与 Controller 或 ApiController 不同,当我指定 AllowInsecureHttp = false 时,我不知道如何允许这些转发的请求通过。

具体来说,在应用启动/配置中:

  app.UseOAuthBearerTokens(new OAuthAuthorizationServerOptions {
            AllowInsecureHttp = true,
   });

需要以某种方式在内部进行此检查,如果为真,则将其视为 SSL:

HttpContext.Request.Headers["X-Forwarded-Proto"] == "https"

这是我通过应用自定义过滤器属性使用 MVC 控制器的方法: https ://gist.github.com/runesoerensen/915869

4

1 回答 1

8

您可以尝试注册一些可以根据 nginx 转发的标头修改请求的中间件。您可能还想将远程 IP 地址设置为X-Forwarded-For标头的值。

像这样的东西应该可以工作(未经测试):

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,
});
于 2014-06-14T01:26:35.137 回答