gpt4 book ai didi

c# - 使用 JWT 检索 Azure AD 组信息

转载 作者:行者123 更新时间:2023-11-30 20:24:25 25 4
gpt4 key购买 nike

我有需要 Azure AD 持有者身份验证的 API。

public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
// ...
});
}

然后是否可以查询 Azure AD(可能使用图形 API)以确定调用用户的组信息?这里的最终目标是将基于角色的安全性应用于 API 方法/ Controller ,如下(或类似)。

[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]

此外,身份信息如何以及在何处应用于执行线程?

最佳答案

最近,您可以使用 Role Claims 和/或 Group Claims 来做到这一点。如果您有一个受承载身份验证保护的 Web API(如在 sample here 中),您可以配置 API,以便访问 token 包含组和/或角色声明。

OWIN 中间件将读取 JWT 不记名 token 中的声明,并在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler ( source ).

要配置您的 API 以接收组声明,您需要编辑应用程序 list 的 "groupMembershipClaims" 属性,并将其设为 "All" “SecurityGroups”(分别包含或排除分发列表),如 this sample 所示,它使用组声明将基于角色的安全应用到使用 [Authorize] 标记的 Web 应用程序。

要配置您的 API 以接收角色声明,您还需要编辑 list ,在 "appRoles" 属性中定义应用程序角色,如 this sample 所示(链接尚未激活 - 它将在接下来的几天),它使用 Role Claims 来做同样的事情。定义应用程序角色后,您可以在 Azure 门户中或通过 GraphAPI 将用户和组分配给这些角色。请注意,由于 AAD 发出的声明属于 "roles" 类型,因此您需要将 RoleClaimType 设置为:

new WindowsAzureActiveDirectoryBearerAuthenticationOptions  
{
...
TokenValidationParameters = new TokenValidationParameters {
RoleClaimType = "roles",
},
...
}

关于c# - 使用 JWT 检索 Azure AD 组信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26846446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com