【问题标题】:UserPrincipal.IsMemberOf is returning falseUserPrincipal.IsMemberOf 返回 false
【发布时间】:2014-10-18 21:45:22
【问题描述】:

我正在尝试验证用户是否在“TestGroup”组中。 用户是“TestGroup”组的一部分,即使我得到了 retval = false @line(retVal = user.IsMemberOf(groupPrincipal);),在事件查看器中它显示 msg 为“用户名或密码不正确”。

你能帮帮我吗?

string userName = this.Request.ServerVariables["AUTH_USER"];
if (ValidateUser(userName) == false)
      Response.Redirect("Error.aspx?errormsg=" + userName + " does not have permission to view this page");

 public static bool ValidateUser(String userName)
        {
            bool useGroupAuthorization = true;
            if (useGroupAuthorization)
                return GroupLookup(userName, "TestGroup");            
} 

private static bool GroupLookup(string userName, string groupName)
        {
            System.Diagnostics.EventLog appLog = new System.Diagnostics.EventLog();
            appLog.Source = "Test App";
            bool retVal = false;
            PrincipalContext pc = null;
            UserPrincipal user = null;
            GroupPrincipal groupPrincipal = null;

            try
            {
                string strdomain = "TestDomain"; 
                pc = new PrincipalContext(ContextType.Domain,strdomain);

                user = UserPrincipal.FindByIdentity(pc, userName);

                groupPrincipal = GroupPrincipal.FindByIdentity(pc, groupName);     

                retVal = user.IsMemberOf(groupPrincipal);

            }
            catch (NoMatchingPrincipalException nmpx)
            {              
                appLog.WriteEntry(nmpx.Message);
            }
            catch (PrincipalOperationException pox)
            {
               appLog.WriteEntry(pox.Message);
            }
            catch (Exception ex)
            {
                if (user == null)
                {

                    appLog.WriteEntry(ex.Message);
                }
                else
                {
                    appLog.WriteEntry(ex.Message);
                }
            }
            return retVal;
        }




    // when i tried with below code i am getting userPrincipal is null

     //    bool retVal = false; string strdomain = "TestDomain";
        //    PrincipalContext principalCtx = new PrincipalContext(ContextType.Domain, strdomain);
        //      UserPrincipal queryByExampleUser = new UserPrincipal ( principalCtx );
        //      queryByExampleUser.SamAccountName = userName;
        //      PrincipalSearcher principalSearcher = new PrincipalSearcher ( );
        //      principalSearcher.QueryFilter = queryByExampleUser;
        //      UserPrincipal userPrincipal = principalSearcher.FindOne ( ) as UserPrincipal;

        //      retVal =  IsUserInGroup("TestGroup", userPrincipal);

        //      return retVal;
        // }

        //static bool IsUserInGroup(string groupName, UserPrincipal user)
        //{
        //    PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
        //    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
        //    if (user.IsMemberOf(groupPrincipal))
        //    {
        //        return true;
        //    }
        //    return false;
        //}

【问题讨论】:

  • 您确定用户是 TestGroup 的一部分,而不是属于 TestGroup 的组的一部分吗? IsMemberOf 不会通过嵌套组递归。
  • 对于迟到的 cmets 感到抱歉。是的,该用户属于该组。

标签: c# asp.net iprincipal principalcontext


【解决方案1】:

“gpKnownAccountToCheck.Members”不是递归的。

需要使用方法:GetMembers(recursive: true)

 var result = groupPrincipal
                    .GetMembers(true)
                    .Where(x => x.Sid == userPrincipal.Sid)
                    .Count() > 0;

【讨论】:

    【解决方案2】:

    UserPrincipal.IsMemberOf(GroupPrincipal) 似乎适用于某些组而不是其他组。在我的域上,它与域\开发人员(自定义组)一起使用,但不是域\域用户。去搞清楚。我在调试器中停止了代码并检查了域用户组的成员列表,并在其中找到了我的用户,但 IsMemberOf 仍然返回 false。但是,我发现如果我遍历 GroupPrincipal.Members 中的 UserPrincipal 对象集合,我可以通过将集合中的 UserPrincipal 与我正在搜索的对象进行比较来进行检查。糟糕,但我能找到唯一可靠的解决方案。

    示例代码:

    string sAccountToCheckSID = upAccountToCheck.Sid.Value;
    foreach(UserPrincipal up in gpKnownAccountToCheck.Members)
    {
         string sKnownSIDInGroup = up.Sid.Value;
         if(sKnownSIDInGroup.Equals(sAccountToCheckSID))
         {
              userMatchingUserObject = userKnownUser;
              return true;
         }
    }
    

    所以我不知道为什么。但这是我的工作。

    【讨论】: