【问题标题】:.Net Core 2.0 - Azure Active Directory - NGinX reverse Proxy - HTTPS.Net Core 2.0 - Azure Active Directory - NGinX 反向代理 - HTTPS
【发布时间】:2018-08-26 17:50:59
【问题描述】:

我有一个 .NET Core 2.0 网站,在 Linux 上运行,监听端口 5000,在 NGinX 反向代理后面,监听端口 80 和 442。NGinX 配置为将 HTTP 流量重定向到 HTTPS,并处理所有通信通过 HTTPS。 NGinx 反向代理和 Asp.Net Core 应用程序位于它们自己的 docker 容器中,位于共享网络上。

此设置当前正在按说明工作。

但是,我现在想针对我们的 Azure Active Directory 对我的用户进行身份验证。我遇到了一个问题,不知道从哪里开始。

首先,让我向您展示我是如何配置大部分内容的,然后我将解释我的错误。

NGinx (nginx.conf)

worker_processes 2;

events { worker_connections 1024; }

http {
    sendfile on;

    upstream docker-dotnet {
        server mycomp_prod:5000;
    }

    server {
            listen 80;
            server_name sales.mycompany.com;
            return 301 https://$host$request_uri;
    }

    server {
        listen 443 ssl;
        server_name sales.mycompany.com;
        ssl_certificate     /etc/nginx/ssl/combined.crt;
        ssl_certificate_key /etc/nginx/ssl/salesmycompany.key;

        location / {
            proxy_pass         http://docker-dotnet;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }

}

Dotnet:Startup.cs ConfigureServices()

services.AddAuthentication(sharedOptions =>
{
     sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
     sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAd(options => Configuration.Bind("AzureAd", options))
.AddCookie();

Dotnet:Startup.cs Configure()

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();

Dotnet:appsettings.json

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "mycompany.com",
    "TenantId": "my-tenant-id",
    "ClientId": "my-client-id",
    "CallbackPath": "/signin-oidc"
  },

在我的 Azure Active Directory 租户中

我已经配置了 2 个“回复 URL”

现在是错误/我的问题

当我访问该站点时,我会登录。然后登录并选择是否“保持登录状态”,我被带到一个错误的页面。

您可以在错误中看到,尝试重定向到的回复地址是 HTTP,而不是 HTTPS。我认为这是来自 appsettings.json 中的一行:

"CallbackPath": "/signin-oidc"

由于我的 .NET Core 站点(通过端口 5000 上的 Kestrel)不在 HTTPS 上,因此它正在尝试加载 http://example.com/signin-oidc,而不是 HTTPS。请记住,我的 .NET Core 站点位于 NGinX 反向代理后面,该代理配置为处理 443 上的流量。

谢谢,

【问题讨论】:

  • appsettings.json 中的 BaseUrl 是什么?应阅读 https://...,然后在与 AAD 回复 URL 匹配时附加 CallbackPath。我在您的列表中没有看到它,所以请尝试添加它。
  • 在 appsettings.json 中,“BaseUrl”是“AzureAd”上的属性,还是在其他对象下?我查看了 docs.microsoft.com,似乎找不到有关此属性的文档。你能带我看一下这个属性的文档吗?

标签: azure asp.net-core azure-active-directory


【解决方案1】:

您应该查看Hosting on Linux 上的文档。

确保在UseAuthentication之前在中间件管道中使用UseForwardedHeaders

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseAuthentication();

Nginx 会在将调用转发到 Kestrel 之前为使用的协议分配一个标头(除其他外)。 然后,此中间件将检查这些标头以设置正确的协议。

【讨论】:

  • 我应该提到它,但在我调用 app.UseAuthentication(); 之前,我的 Configure() 方法中确实有该代码;我将更新我的问题以包含该代码块。
  • 好吧 :) 无论如何我都会在这里保留答案,因为它可能对遇到类似问题的其他人有用。
  • 谢谢。但是您还有其他想法吗?
  • 这是我想到的第一件事,现在没有别的想法了。如果我有什么想法,我会回复你。
  • 需要设置 x-forwarded-proto 标头。
【解决方案2】:

在 startup.cs 中,您可以尝试强制使用主机名和架构。以下代码可能会有所帮助。重要提示:将其放置在设置身份验证之前。

app.Use((context, next) =>
{
   context.Request.Host = new HostString(hostname + (port.HasValue ? ":" + port : null));
   context.Request.Scheme = protocol;
   return next();
});

【讨论】:

    【解决方案3】:

    我看到了这个并且能够让它工作:https://github.com/aspnet/Security/issues/1702

            var fordwardedHeaderOptions = new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            };
            fordwardedHeaderOptions.KnownNetworks.Clear();
            fordwardedHeaderOptions.KnownProxies.Clear();
    
            app.UseForwardedHeaders(fordwardedHeaderOptions);
    

    【讨论】:

    • 你先生,太棒了!我花了几个小时试图弄清楚这一点,而您的代码 sn-p 成功了。谢谢。
    • 在我对 nginx 的测试中,它只有助于配置 XForwardedProto 而不是 XForwardedFor。无需清除任何东西,我怀疑这会导致其他问题。来源:github.com/aspnet/Security/issues/1702#issuecomment-377396765
    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多