gpt4 book ai didi

c# - IsUserInRole 调用 GetRolesForUser?

转载 作者:可可西里 更新时间:2023-11-01 08:42:43 27 4
gpt4 key购买 nike

当我实现 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com