【发布时间】:2018-11-01 14:50:21
【问题描述】:
我已经有一个使用 ASP.Net Core 2.1 的 AAD 集成 WebAPP,但现在我想使用 ASPNet Core 2.1 开发一个 API,以使用 JWT 不记名令牌向我的 api 验证 AAD 用户。我无法执行相同的操作,因为在 Web 应用程序中我使用的是 Cookie Auth 模式,但在这里我需要实现对我不起作用的 JWT Bearer。我尝试了很多来自不同代码仓库的代码。
参考资料: https://github.com/juunas11/Joonasw.AzureAdApiSample
https://github.com/Azure-Samples/active-directory-b2c-dotnetcore-webapi
https://azure.microsoft.com/en-in/resources/samples/active-directory-dotnet-native-aspnetcore-v2/
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = "https://localhost:44395/";
options.Authority = "https://localhost:44395/identity/";
})
.AddJwtBearer("AzureAD", options =>
{
options.Audience = "https://localhost:44395/";
options.Authority = "https://login.microsoftonline.com/tenantID/";
});
services.AddAuthorization(options =>
{
var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(
JwtBearerDefaults.AuthenticationScheme,
"AzureAD");
defaultAuthorizationPolicyBuilder =
defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();
options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build();
});
当我将模式更改为 Cookie 模式时,它可以正常工作,但不能在 JWTBearer 代码中工作。
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes (Bearer, AzureAD).
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: AuthenticationScheme: Bearer was challenged.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: AuthenticationScheme: AzureAD was challenged.
有人可以帮我吗?由于这个问题,我被困在这里。
提前致谢
【问题讨论】:
标签: azure asp.net-core