代码之家  ›  专栏  ›  技术社区  ›  J c

Roles.IsUserInRole是否在以下简单场景中按预期运行?

  •  3
  • J c  · 技术社区  · 17 年前

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

    Roles.IsUserInRole("any username", "any rolename"); // results in true
    

    但是,以下代码返回false:

    Roles.IsUserInRole("any rolename"); // results in false
    

    请注意,User.IsInRole(“任何rolename”)也返回false。

    1. 这是预期的行为吗?
    2. 假设仅采用角色名称的重载仍将调用重写的IsUserInRole,这是错误的吗?

    使现代化 :请注意,对于采用单个字符串的版本,似乎没有可用的覆盖,这导致了我在#2中的假设。

    3 回复  |  直到 17 年前
        1
  •  3
  •   Andrew Rollings    17 年前

    public static bool IsUserInRole(string roleName)
    {
        return IsUserInRole(GetCurrentUserName(), roleName);
    }
    

    我会看看你现在的用户。原因如下:

    private static string GetCurrentUserName()
    {
        IPrincipal currentUser = GetCurrentUser();
        if ((currentUser != null) && (currentUser.Identity != null))
        {
            return currentUser.Identity.Name;
        }
        return string.Empty;
    }
    

    我敢打赌这将返回一个空字符串,因为您没有当前用户,或者其名称为空字符串或null。

    IsUserInRole(string username, string roleName) 方法,则在开头附近有以下代码块:

       if (username.Length < 1)
       {
           return false;
       }
    

    GetCurrentUserName() 如果不返回任何有意义的内容,那么它将在调用重写的方法之前返回false。

        2
  •  0
  •   Rebecca    16 年前

    如果在RoleManager配置中选择了cacheRolesInCookie=“true”,也要小心。如果向数据库中添加了新角色,它可能正在查看cookie中的缓存版本。

    我遇到了这个问题,解决方法是删除cookie并重新登录。

        3
  •  0
  •   FiveTools    16 年前

    这可能有助于某人-注意:

    如果您使用登录控件进行身份验证,则输入控件的用户名将成为HttpContext.Current.User.Identity.Name,该名称用于Roles.IsUserInRole(字符串rolename),更具体地说,是成员身份的GetUser()方法。因此,如果是这种情况,请确保覆盖Authenticate事件,验证此方法中的用户,并将用户名设置为自定义成员资格提供程序可以使用的值。

     protected void crtlLoginUserLogin_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool blnAuthenticate = false;
        string strUserName = crtlLoginUserLogin.UserName;
    
        if (IsValidEmail(strUserName))
        {
    
            //if more than one user has email address - must authenticate by username.
    
            MembershipUserCollection users = Membership.FindUsersByEmail(strUserName);
            if (users.Count > 1)
            {
                crtlLoginUserLogin.FailureText = "We are unable to determine which account is registered to that email address. Please enter your Username to login.";
    
            }
            else
            {
                strUserName = Membership.GetUserNameByEmail(strUserName);
                blnAuthenticate = Membership.ValidateUser(strUserName, crtlLoginUserLogin.Password);
    
                //setting the userLogin to the correct user name (only on successful authentication)
                if (blnAuthenticate)
                {
                    crtlLoginUserLogin.UserName = strUserName;
                }
    
            }
    
    
        }
        else
        {
            blnAuthenticate = Membership.ValidateUser(strUserName, crtlLoginUserLogin.Password);
        }
    
        e.Authenticated = blnAuthenticate;
    
    }
    
    推荐文章