"User.IsInRole"中的多个角色 [英] Multiple roles in 'User.IsInRole'

查看:70
本文介绍了"User.IsInRole"中的多个角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有3个角色,所以我想访问两个角色的链接.

I have 3 roles on my page, so I want to get access to link with two roles.

我尝试这样的事情

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

或者这个

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

{
 //my code
}

没有人工作,我唯一设法完成的工作是:

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

 @if (User.IsInRole("Admin")

但这最后一个仅适用于一个角色,我该怎么做?

But this last one it's only for one Role, How can I do that I want?

推荐答案

没有人工作,我唯一设法完成的工作是:

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

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

This is reasonable, if you consider what the method IsInRole does.

获取一个值,该值指示当前登录的用户是否在指定角色.该API仅可在ASP.NET请求线程的上下文,以及在该批准的用例中这是线程安全的.

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 .

That being said a user may has the role of Admin (so UserIsInRole("Admin") returns true) but may not has the role of User (so UserIsInRole("User") returns false). So User.IsInRole("Admin") && User.IsInRole("User") would evaluate to false.

您可能需要的是这个

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

这篇关于"User.IsInRole"中的多个角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆