'User.IsInRole' 中的多个角色

Multiple roles in 'User.IsInRole'

我的页面上有 3 个角色,所以我想通过两个角色访问 link。

我试试这样的

@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.

话虽如此,用户可能具有管理员角色(因此 UserIsInRole("Admin") returns 正确)但可能 具有用户角色(因此UserIsInRole("User") returns 错误)。所以 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
}