【发布时间】: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