【发布时间】:2019-01-27 21:06:59
【问题描述】:
我有一个 webapp,它支持基于表单的身份验证(使用 OOTB Asp.net 身份框架)和基于 Azure AD 的身份验证(使用 OpenID Connect)。身份验证适用于基于表单的用户和 Azure AD 用户。现在角色在数据库中(使用 OOTB Asp.net 身份框架),并且角色作为所有 asp.net 身份用户的声明获得。但对于 Azure AD 身份用户,我无法获得角色声明。我尝试使用电子邮件查询数据库并获取 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 AD 身份中。
如何使用 asp.net 身份角色(而不是 Azure AD 角色)管理的角色来实现 Azure AD 身份验证
请注意,目前我不能选择使用 Azure AD 角色,因为这是具有 asp.net 身份和角色的现有应用程序。我正在尝试以最少的更改再添加一个身份验证提供程序。所以角色必须像现在一样在数据库中。
【问题讨论】:
-
嗯,你不会使用
UserManager的函数吗?然后它应该将数据添加到数据库中以将其持久化。
标签: asp.net asp.net-mvc azure-active-directory openid-connect asp.net-identity-3