nginx – AspNetCore Azure AD Connect回调URL是http,而不是https

我有一个AspNet Core 2.0应用程序,它使用OpenIdConnect API授权用户使用Azure AD. Azure应用程序条目的回调uris定义为https://localhost:44369/signin-oidc和https://domain.tld/signin-oidc.当我使用IIS Express在l...

我有一个AspNet Core 2.0应用程序,它使用OpenIdConnect API授权用户使用Azure AD. Azure应用程序条目的回调uris定义为https://localhost:44369/signin-oidc和https://domain.tld/signin-oidc.当我使用IIS Express在localhost上部署我的应用程序时,一切正常,我可以正确地验证用户.

当我将我的应用程序部署到Linux系统时,Nginx被配置为应用程序的反向代理,身份验证不起作用. Azure AD显示以下错误消息:

AADSTS50011: The reply address ‘07002’ does
not match the reply addresses configured for the application. More
details: not specified

显然,我的应用程序告诉Azure AD重定向回http地址,Azure AD拒绝这样做(幸运的是).我想问题是我的应用程序认为它使用http,因为它在http://localhost:5000/上侦听反向代理.

public void Configure(string name, OpenIdConnectOptions options)
{
    options.ClientId = _azureOptions.ClientId;
    options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
    options.UseTokenLifetime = true;
    options.CallbackPath = _azureOptions.CallbackPath;
    options.RequireHttpsMetadata = true;
}

这是我用来配置OpenIdConnect的代码.在异常中指定CallbackPath的绝对路径.有没有其他方法告诉OpenIdConnect始终使用https作为CallbackPath?

如果我的Nginx配置不正确,这是我配置的一部分:

location / {
        # redirect to ASP.NET application
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $http_host;
        proxy_cache_bypass $http_upgrade;
}

任何帮助都非常感谢!

解决方法:

我找到一个链接问题,帖子解决了这个问题.该帖子在app.UseAuthentication()之前指示了以下插入;

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

    app.UseForwardedHeaders(fordwardedHeaderOptions);

将此与您的Nginx配置相结合;它必须转发这些信息:

        #
        # Proxy WEB
        #
        location / {
                proxy_pass http://<snip>;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
        }

为了彻底,我已经尝试转发标题信息,但无济于事.这两行做了诀窍:

fordwardedHeaderOptions.KnownNetworks.Clear();
fordwardedHeaderOptions.KnownProxies.Clear();

感谢charlierlee over in this post.

本文标题为:nginx – AspNetCore Azure AD Connect回调URL是http,而不是https

基础教程推荐