我在azure Ad Web API中登录后无法获取ClaimsPrincipal,以下是我在startup.auth.cs 中添加的代码
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
PostLogoutRedirectUri = redirectUri,
Scope= OpenIdConnectScope.OpenIdProfile,
ResponseType = OpenIdConnectResponseType.IdToken,
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
return Task.FromResult(0);
}
},
TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = false
},
});
我在中获得访问令牌
result = await authContext.AcquireTokenAsync(todoListResourceId, clientCredential);
但无法获得ClaimsPrincipal。我得到AuthenticationType=null,IsAuthenticated=null,Name=null。
我的应用程序使用adal.js作为UI端来获取用户信息,并成功获取用户信息。
我得到了这个问题的解决方案。用替换startup.auth.cs代码
app.UseWindowsAzureActiveDirectoryBearerAuthentication( new WindowsAzureActiveDirectoryBearerAuthenticationOptions { TokenValidationParameters = new TokenValidationParameters { SaveSigninToken = true, ValidAudience = ConfigurationManager.AppSettings["ida:ClientId"], AuthenticationType = "Bearer" }, Tenant = ConfigurationManager.AppSettings["ida:Tenant"], });
这个代码和它工作良好