加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – IClaimsTransformer的User.IsInRole(“Administ

发布时间:2020-12-16 03:45:52 所属栏目:asp.Net 来源:网络整理
导读:我使用IClaimsTransformer impelimentation在身份验证后将角色管理员添加到用户声明中,如下所示: (principal.Identity as ClaimsIdentity).AddClaim(new Claim(ClaimTypes.Role,“Administrators”)); 但是当我在Razor视图中调用User.IsInRole(“Administra
我使用IClaimsTransformer impelimentation在身份验证后将角色管理员添加到用户声明中,如下所示:

(principal.Identity as ClaimsIdentity).AddClaim(new Claim(ClaimTypes.Role,“Administrators”));
但是当我在Razor视图中调用User.IsInRole(“Administrators”)时,它返回false.

解决方法

我在基于API的解决方案中做了类似的事情,但是在创建用户时设置了角色声明,而不是在变换器中设置.

在查看User.IsInRole()documentation后,看起来此方法旨在首先从缓存中提取.

IsInRole first checks the IsRoleListCached property to determine
whether a cached list of role names for the current user is available.
If the IsRoleListCached property is true,the cached list is checked
for the specified role. If the IsInRole method finds the specified
role in the cached list,it returns true. If IsInRole does not find
the specified role,it calls the GetRolesForUser method of the default
Provider instance to determine whether the user name is associated
with a role from the data source for the configured ApplicationName
value.

我怀疑ClaimTypes.Role是一个常见的声明,而不是自定义的特定于域的声明(我认为是ClaimsTransformer的用例),应用程序使用默认的,缓存的,预转换值.

但大部分都是猜测.您可以在创建用户时尝试设置声明.我是使用UserManager类做的.

var claimsResult = await _userManager.AddClaimAsync(applicationUser,new Claim(ClaimsIdentity.DefaultRoleClaimType,"Administrator"));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读