【问题标题】:With google auth, how do I setup asp.net core to request https redirects when asp.net core is running http only, but reverse proxy runs https only?使用 google auth,当 asp.net core 仅运行 http,但反向代理仅运行 https 时,如何设置 asp.net core 以请求 https 重定向?
【发布时间】:2018-11-26 00:16:19
【问题描述】:

以上配置有一个使用google认证的asp.net core app。但由于某种原因,向 Google 的身份验证重定向使用 http 而不是 https 发送重定向 URI。无论我在哪里看,似乎都没有办法在中间件中改变它。

在 apache 方面,我遵循了一个据称使用

转发协议的教程
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}

但是它不能正常工作。下面的更多信息和有趣的安全后果。

现在,由于 kestrel 正在运行 http,它默认为 google 身份验证设置“重定向 URI”以使用 http 协议。

我在上面的场景中使用了 http 重定向 URI。我已经对此进行了Wireshark,它适用于HTTP重定向URI!嗯,我只允许 https 进入。所以我 tcpdump 我的服务器上的交互,我发现因为 apache 需要 https,它会抛出一个永久移动到 https 的 HTTP 301。太好了,这就是我所期望的。我没想到的是谷歌会重定向到 https 协议。好的,不理想,但它有效,所以我为什么要问?它首先通过 http 发送数据以获取 301,所以那时我已经失去了加密。如果有人在窥探,他们可以阅读谷歌的全部回复。 IE。我可以使用 tcpdump 在 http 上看到 google 回帖。

以下是唯一与身份验证相关的代码:

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddGoogle(options =>
        {
            options.ClientId = Configuration["Authentication:Google:ClientId"];
            options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
        })
        .AddCookie(options =>
        {
            options.LoginPath = "/auth/signin";
        });

注意,在我的代码中我没有使用 app.UseHttpsRedirection();

【问题讨论】:

    标签: apache asp.net-core reverse-proxy google-authentication asp.net-core-2.1


    【解决方案1】:

    虽然,我仍然有兴趣找到一种方法来更改使用“.AddGoogle”身份验证从 asp.net 核心发送到 google 的重定向 URI 的值,但我能够通过更改 apache 配置来解决问题到:

    RequestHeader set "X-Forwarded-For"
    RequestHeader set "X-Forwarded-Proto" "https"
    

    本质上是对协议进行硬编码,迫使它将“https”传递给 asp.net 核心中间件,现在它可以正常工作。

    【讨论】:

    • 我遇到了类似的问题。你是在中间件里设置成 https 吗?
    • 本质上没有。我不得不删除那个样板代码。所以,我的 Startup.cs 文件中确实没有app.UseHttpsRedirection(); 语句。我确实在我的 startup.cs 文件中添加了以下内容:app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); 您还必须将答案中提到的 X-Forwarded-For 和 X-Forwarded-Proto 值设置为 apache 配置的一部分。
    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2018-02-22
    • 2016-09-30
    • 2019-03-27
    相关资源
    最近更新 更多