ASP.NET 5 OAuth 重定向 URI 不使用 HTTPS

opi*_*kie 7 asp.net-core

我正在复制社交示例,但尝试使用此处未显示的不同 OAuth 提供程序,因此我有一些如下代码:

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

我的应用程序在终止 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);
    }
});
Run Code Online (Sandbox Code Playgroud)

Kév*_*let 9

推荐的方法是手动更改context.Request.Scheme为 returnhttps而不是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);
});
Run Code Online (Sandbox Code Playgroud)

仅供参考,ASP.NET 团队目前正在开发一个可以自动执行此任务的中间件: https: //github.com/aspnet/BasicMiddleware/pull/1/files