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

ASP.NET 5 OAuth redirect URI not using HTTPS

我正在复制 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 的 reverse-proxy 后面。然后我的 redirect_uri 变成了 http://myapp.com/signin-test when I need it to be https://myapp.com/signin-test

应用程序只知道 X-Forwarded-Proto HTTP header 是否设置为 "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);
    }
});

推荐的方法是在调用管道的其余部分 (await next()) 之前手动将 context.Request.Scheme 更改为 return 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