我有一个webapp,它支持基于表单的身份验证(使用OOTB Asp.net identity framework)和基于Azure广告的身份验证(使用OpenID Connect)。身份验证对基于表单的用户和Azure AD用户都很有效。现在,这些角色在数据库中(使用OOTB Asp.net identity framework),这些角色作为所有Asp.net的声明获得。net身份用户。但对于Azure广告标识用户,我无法获得角色声明。我尝试通过电子邮件查询数据库,获取Azure AD用户的角色,然后将其设置为角色声明,如下所示:
User.SetClaims(ClaimTypes.Role,roleAdministrator.Name);//calling the set method
将声明设置如下。
public static void SetClaims(this IPrincipal user,string claimType,string claimValue)
{
if (user?.Identity is ClaimsIdentity claimsIdentity)
{
var name = claimsIdentity.FindFirst(claimType);
if (name != null)
claimsIdentity.RemoveClaim(name);
claimsIdentity.AddClaim(new Claim(claimType, claimValue));
}
}
这只适用于特定的http请求,只要我单击其他地方或刷新页面,角色声明就不再存在于Azure广告标识中。
如何使用asp管理的角色实现Azure AD auth。网络身份角色(而不是Azure广告角色)
请注意,使用Azure广告角色目前不是我的选择,因为这是一个带有asp的现有应用程序。net身份和角色。我正在尝试以最小的更改再添加一个身份验证提供程序。因此,角色必须像现在一样存在于数据库中。