在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:信息:请求启动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:Debug:Connection id&#34; 0HLC9LOBLM019&#34;,Request id&#34; 0HLC9LOBLM019:00000004&#34;:开始读取请求体。 Microsoft.AspNetCore.Server.Kestrel:Debug:Connection id&#34; 0HLC9LOBLM019&#34;,Request id&#34; 0HLC9LOBLM019:00000004&#34;:开始读取请求体。 Microsoft.AspNetCore.Server.Kestrel:Debug:连接ID&#34; 0HLC9LOBLM019&#34;,请求ID&#34; 0HLC9LOBLM019:00000004&#34;:完成读取请求正文。 Microsoft.AspNetCore.Server.Kestrel:Debug:连接ID&#34; 0HLC9LOBLM019&#34;,请求ID&#34; 0HLC9LOBLM019:00000004&#34;:完成读取请求正文。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的取消保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler&#39;,& #39; aad&#39;,&#39; v1&#39;)。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的取消保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler&#39;,& #39; aad&#39;,&#39; v1&#39;)。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Trace:MessageReceived:&#39;?id_token = {keyremoved}&#39;。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Trace:MessageReceived:&#39;?id_token = {keyremoved}&#39;。 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Debug:更新配置 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Debug:更新配置 Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Debug:Received&#39; id_token&#39; Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler:Debug:Received&#39; id_token&#39; Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的取消保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler&#39;,& #39; System.String&#39;,&#39; aad&#39;,&#39; v1&#39;)。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的取消保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler&#39;,& #39; System.String&#39;,&#39; aad&#39;,&#39; v1&#39;)。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware&#39;,& #39; Cookies&#39;,&#39; v2&#39;)。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware&#39;,& #39; Cookies&#39;,&#39; v2&#39;)。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:已登录的Cookie。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:已登录的Cookie。 Microsoft.AspNetCore.Server.Kestrel:Debug:Connection id&#34; 0HLC9LOBLM019&#34;完成保持活跃的反应。 Microsoft.AspNetCore.Server.Kestrel:Debug:Connection id&#34; 0HLC9LOBLM019&#34;完成保持活跃的反应。 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求以129.6921ms 302结束 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求以129.6921ms 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:跟踪:执行对密钥{keyremoved}的取消保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware&#39;,& #39; Cookies&#39;,&#39; v2&#39;)。 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector:跟踪:执行对密钥{keyremoved}的取消保护操作(&#39;工作区&#39;,&#39; Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware&#39;,& #39; Cookies&#39;,&#39; v2&#39;)。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:Cookie已成功通过身份验证。 Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:AuthenticationScheme:Cookie已成功通过身份验证。 Microsoft.AspNetCore.Routing.Tree.TreeRouter:Debug:请求成功匹配路由名称&#39;授权&#39;和模板&#39; api / connect / authorize&#39;。 Microsoft.AspNetCore.Routing.Tree.TreeRouter:Debug:请求成功匹配路由名称&#39;授权&#39;和模板&#39; api / connect / authorize&#39;。
答案 0 :(得分: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");
在这种情况下,用户将在处理登录后重定向到应用程序的根目录。