微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

参见英文答案 > 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
}

原文地址:https://www.jb51.cc/csharp/98920.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐