【问题标题】:AddOpenIdConnect with Azure AD V2AddOpenIdConnect 与 Azure AD V2
【发布时间】:2018-08-23 03:16:23
【问题描述】:

net core 2.0 和 Azure AD (V2) 中的 AddOpenIdConnect 存在一些问题。在挑战之后,在 Azure AD 中输入凭据并返回到我的应用程序,身份验证处理程序似乎将我重定向到发起挑战的原始方法,而不是定义的回调方法。 但是,httpcontext.user 填充了一个声明标识,其中包含正确的声明。

为了这篇文章的目的,代码被简化了。

启动看起来像:

            services.AddAuthentication(o =>
            {
                o.DefaultChallengeScheme = "aad";
                o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
            {
                if (!Environment.IsProduction())
                {
                    o.RequireHttpsMetadata = false;
                }
                o.Authority = Configuration.GetValue<string>("Authentication:Authority");
                o.Audience = Constants.Audiences.Self;
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = OpenIdConnectConstants.Claims.Subject,
                    RoleClaimType = OpenIdConnectConstants.Claims.Role,
                    IssuerSigningKey =
                        new X509SecurityKey(
                            GetSigningCertificate(Configuration.GetValue<string>("Certificates:Signing")))
                };
            })
            .AddCookie()
            .AddOpenIdConnect("aad", o =>
            {
                if (!Environment.IsProduction())
                {
                    o.RequireHttpsMetadata = false;
                }
                o.Authority = "https://login.microsoftonline.com/{tenantID}/v2.0";
                o.AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet;
                o.ClientId = "[clientid]";
                o.ClientSecret = "[clientsecret]";
                o.ResponseMode = "form_post";
                o.ResponseType = "id_token";
                o.CallbackPath = new PathString("/api/connect/microsoftcallback2");
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer = "https://login.microsoftonline.com/{tenantID}/v2.0"
                };
            });

挑战:

    [AllowAnonymous]
    [HttpGet("authorize", Name = "authorize")]
    public async Task<IActionResult> ChallengeTemp()
    {
        return Challenge("aad");
    }

跟踪日志:

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: 请求开始 HTTP/1.1 POST https://localhost:44301/api/connect/microsoftcallback2 application/x-www-form-urlencoded 1771 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 POST https://localhost:44301/api/connect/microsoftcallback2 application/x-www-form-urlencoded 1771 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Trace:进入 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler 的 HandleRemoteAuthenticateAsync。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Trace:进入 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler 的 HandleRemoteAuthenticateAsync。 Microsoft.AspNetCore.Server.Kestrel:调试:连接 ID“0HLC9LOBLM019”,请求 ID“0HLC9LOBLM019:00000004”:开始读取请求正文。 Microsoft.AspNetCore.Server.Kestrel:调试:连接 ID“0HLC9LOBLM019”,请求 ID“0HLC9LOBLM019:00000004”:开始读取请求正文。 Microsoft.AspNetCore.Server.Kestrel:调试:连接 ID“0HLC9LOBLM019”,请求 ID“0HLC9LOBLM019:00000004”:完成读取请求正文。 Microsoft.AspNetCore.Server.Kestrel:调试:连接 ID“0HLC9LOBLM019”,请求 ID“0HLC9LOBLM019:00000004”:完成读取请求正文。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:Trace:对密钥 {keyremoved} 执行解除保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler'、'aad'、'v1')。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:Trace:对密钥 {keyremoved} 执行解除保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler'、'aad'、'v1')。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Trace: MessageReceived: '?id_token={keyremoved}'。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Trace: MessageReceived: '?id_token={keyremoved}'。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Debug:更新配置 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Debug:更新配置 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:调试:收到“id_token” Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:调试:收到“id_token” Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:对密钥 {keyremoved} 执行解除保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler'、'System.String'、'aad'、' v1')。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:对密钥 {keyremoved} 执行解除保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler'、'System.String'、'aad'、' v1')。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:Trace:对密钥 {keyremoved} 执行保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware'、'Cookies'、'v2')。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:Trace:对密钥 {keyremoved} 执行保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware'、'Cookies'、'v2')。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:已登录 Cookie。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:已登录 Cookie。 Microsoft.AspNetCore.Server.Kestrel:调试:连接 ID“0HLC9LOBLM019”完成保持活动响应。 Microsoft.AspNetCore.Server.Kestrel:调试:连接 ID“0HLC9LOBLM019”完成保持活动响应。 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求在 129.6921 毫秒内完成 302 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求在 129.6921 毫秒内完成 302 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 GET https://localhost:44301/api/connect/authorize
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求开始 HTTP/1.1 GET https://localhost:44301/api/connect/authorize
Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:Trace:对密钥 {keyremoved} 执行取消保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware'、'Cookies'、'v2')。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:Trace:对密钥 {keyremoved} 执行取消保护操作,目的是('workspace'、'Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware'、'Cookies'、'v2')。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:Cookie 已成功通过身份验证。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:Cookie 已成功通过身份验证。 Microsoft.AspNetCore.Routing.Tree.TreeRouter:Debug:请求成功匹配名称为“authorize”和模板为“api/connect/authorize”的路由。 Microsoft.AspNetCore.Routing.Tree.TreeRouter:Debug:请求成功匹配名称为“authorize”和模板为“api/connect/authorize”的路由。

【问题讨论】:

    标签: azure-active-directory asp.net-core-2.0 openid-connect


    【解决方案1】:

    如果你仔细查看你的日志,你会发现它是这样写的:

    请求开始 HTTP/1.1 POST https://localhost:44301/api/connect/microsoftcallback2

    由于您指定了响应应该以 POST 的形式出现:

    o.ResponseMode = "form_post";
    

    您在定义的回调中以 POST 形式获得响应。

    现在,默认情况下,OIDC 处理程序会将您重定向回发出质询时尝试访问的路径。你也可以在日志中看到 302 重定向。

    在显式登录处理程序的情况下,这是不好的。 你应该自己定义它:

    return Challenge(new AuthenticationProperties
    {
        RedirectUri = "/"
    }, "aad");
    

    在这种情况下,用户将在登录处理后被重定向到应用程序的根目录。

    【讨论】:

    • 我错误地认为定义的回调路径将被处理程序用于重定向,但我现在看到它似乎只用于处理程序侦听请求,我需要将其与质询一起发送。谢谢。
    • 是的,回调路径用作来自 AAD 的响应的 URL。这里的redirect是鉴权完成后使用的“本地重定向URL”。
    猜你喜欢
    • 2019-06-26
    • 2017-01-09
    • 2020-05-14
    • 2019-03-31
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多