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

c# – ‘User.IsInRole’中的多个角色

发布时间:2020-12-15 18:34:09 所属栏目:百科 来源:网络整理
导读:参见英文答案 Usage of User.IsInRole() in a View2个 我的页面上有3个角色,因此我希望能够访问与两个角色的链接. 我尝试这样的事情 @if(User.IsInRole("Admin,User")) { //my code} 或这个 @if (User.IsInRole("Admin") User.IsInRole("User")){ //my code}
参见英文答案 > Usage of User.IsInRole() in a View2个
我的页面上有3个角色,因此我希望能够访问与两个角色的链接.

我尝试这样的事情

@if(User.IsInRole("Admin,User")) 
{
 //my code
}

或这个

@if (User.IsInRole("Admin") && User.IsInRole("User"))

{
 //my code
}

没有人工作,我唯一能成功的是:

@if (User.IsInRole("Admin")

但是最后一个只有一个角色,我该怎么做呢?

解决方法

No one work’s,the only one who I managed to works is this:

如果你考虑IsInRole的方法,这是合理的.

Gets a value indicating whether the currently logged-on user is in the
specified role. The API is only intended to be called within the
context of an ASP.NET request thread,and in that sanctioned use case
it is thread-safe.

也就是说,用户可能具有Admin的角色(因此UserIsInRole(“Admin”)返回true)但可能没有User的角色(因此UserIsInRole(“User”)返回false).所以User.IsInRole(“Admin”)&& User.IsInRole(“User”)将评估为false.

您可能需要的是:

// If the user's role is either admin or user then do something
@if (User.IsInRole("Admin") || User.IsInRole("User"))
{
    //my code
}

(编辑:李大同)

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

    推荐文章
      热点阅读