【问题标题】:ASP.NET 5 OAuth redirect URI not using HTTPSASP.NET 5 OAuth 重定向 URI 不使用 HTTPS
【发布时间】:2015-07-08 04:02:40
【问题描述】:

我正在复制Social Sample,但尝试使用此处未显示的其他 OAuth 提供程序,所以我有一些类似这样的代码:

app.UseOAuthAuthentication("test", options =>
{
     options.ClientId = "xxx";
     options.ClientSecret = "xxx";
     options.AuthorizationEndpoint = "xxx";
     options.TokenEndpoint = "xxx";
     options.CallbackPath = new PathString("/signin-test");
});

我的应用在终止 SSL 的反向代理后面运行。当我需要它为https://myapp.com/signin-test 时,我的redirect_uri 然后变成http://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);
    }
});

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    推荐的方法是在调用管道的其余部分 (await next()) 之前手动将 context.Request.Scheme 更改为返回 https 而不是 http

    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

    【讨论】:

    • 有关使用中间件的更多信息,请参阅docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl
    • 该文档链接似乎与根本问题无关;在负载均衡器后面,请求是 HTTP,因此从路径或路由生成的 URL 都返回 HTTP 方案。上面的技巧是假装转发的请求通过 HTTPS。
    猜你喜欢
    • 2017-09-11
    • 2019-03-21
    • 2017-12-10
    • 1970-01-01
    • 2016-09-25
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多