azure - 关于使用 ASPNet Core 2.1 Web API 配置 AAD 集成
问题描述
我已经有一个使用 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.
有人可以在这里帮助我吗?由于这个问题,我被困在这里。
提前致谢
解决方案
假设您有用 ASP.NET Core Web API 编写并受 Azure AD 保护的 REST API 资源,客户端(ASP.NET Web 应用程序)可以使用 OpenID Connect 中间件和 Active Directory 身份验证库(ADAL.NET)来获取 JWT使用 OAuth 2.0 协议的登录用户的不记名令牌。
不记名令牌被传递给 Web API,后者使用 JWT 不记名身份验证中间件验证令牌并授权用户,例如,请参阅第一个链接中的代码示例:
services
.AddAuthentication(o =>
{
o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.Authority = Configuration["Authentication:Authority"];
o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
// Both App ID URI and client id are valid audiences in the access token
ValidAudiences = new List<string>
{
Configuration["Authentication:AppIdUri"],
Configuration["Authentication:ClientId"]
}
};
});
上面的代码示例使用 JwtBearerExtensions 来验证访问令牌。您可以单击此处了解有关场景的说明。
推荐阅读
- c# - 对于此操作,WebSocket 处于无效状态(“已中止”)
- java - While 循环不会根据内部参数更新
- docker - Selenium docker 在 Windows 上崩溃(未知错误:DevToolsActivePort 文件不存在)
- javascript - 将两个过滤器与 javascript 结合使用
- c++ - 从 struct tm 转换为 std::time_t 并转换回日期将被更改
- regex - 正则表达式 - 一切都是可选模式,或者只是一切
- php - 检查重复并仅在表中插入唯一项目,但这仅适用于第一项
- laravel - 如何在弹性搜索中使用 multi where 条件
- java - 如何使用 spring.xml 在 JDBC 中配置 postgres sql URL
- python - 在熊猫条件下左移