c# - IsUserInRole 调用 GetRolesForUser?

标签 c# .net roleprovider

当我实现 RoleProvider 类并调用 Roles.IsUserInRole(string username, string roleName) 时,代码执行首先转到方法“GetRolesForUser(string username)”。为什么是这样?当我只是在寻找该用户是否属于一个角色的单一值时,我不想迭代所有角色。这是 .NET 的角色提供程序类的限制,还是我可以做些什么来更多地控制代码的执行?

调用代码如下

if (Roles.IsUserInRole(CurrentUser.UserName, "Teacher")) {

这是 IsUserInRole 的实现

public override bool IsUserInRole(string username, string roleName) { return true; }

但是代码 GetRolesForUser 总是首先实现:

public override string[] GetRolesForUser(string username) {
        string[] roles = GetAllRoles();
        List<string> userRoles = new List<string>();
        foreach (string role in roles) {
            if (IsUserInRole(username, role)) {
                userRoles.Add(role);
            }
        }
        return userRoles.ToArray();
    }

最佳答案

The RoleProvider.IsUserInRole(username, password) is used for checking roles for a given user which is not the current loggon user(for current logon user, it also use the Principal.IsInRole instead). And for RolePrincipal, it always use the GetRolesForUser to cache the roles and do the role checking among the cached role list. (source)

可以使用 Roles.Provider.IsUserInRole 而不是 Roles.IsUserInRole

关于c# - IsUserInRole 调用 GetRolesForUser?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4081618/

相关文章:

c# - 在 C# struct pointer 中返回相同的地址

c# - 有什么方法可以强类型化控件的 Tag 属性吗?

c# - 如何将带小数的数字的外国字符串表示形式转换为 double ?

asp.net - 使用带有自定义角色提供程序的 ASP.NET MVC 4 登录失败时不会出现错误消息?

asp.net - 如何将基于角色的安全异常重定向到自定义页面 (ASP.NET)

c# - 如何使用 CollectionViewSource 正确绑定(bind)(更新)DataGrid

c# - 如何自动应用 generic.xaml 中的数据模板?

c# - 反射 - 作为接口(interface)的类的 GetProperties

c# - SerialPort port.open "The port ' COM 2' does not exist."

.net - Umbraco 7 自定义成员和角色提供者