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
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...