【问题标题】:Can you find an Active Directory User's Primary Group in C#?你能在 C# 中找到 Active Directory 用户的主要组吗?
【发布时间】:2009-07-24 19:57:46
【问题描述】:

我正在开发一个在 Active Directory 中管理用户帐户的应用程序。我尽可能使用 System.DirectoryServices.AccountManagement 命名空间,但我不知道如何确定用户的主要组。当我尝试删除作为用户主要组的组时,我得到了一个例外。这是我当前的代码:

private void removeFromGroup(UserPrincipal userPrincipal, GroupPrincipal groupPrincipal) {
    TODO: Check to see if this Group is the user's primary group.
    groupPrincipal.Members.Remove(userPrincipal);
    groupPrincipal.Save();
}

有没有办法获取用户主要组的名称,以便在尝试从该组中删除用户之前进行一些验证?

【问题讨论】:

    标签: c# c#-3.0 active-directory


    【解决方案1】:

    这是一个相当混乱且复杂的业务 - 但这段代码 sn-p 来自我的 BeaverTail ADSI 浏览器,我完全用 C# 编写(在 .NET 1.1 天)并且众所周知它可以工作 - 不漂亮,但功能齐全:

    private string GetPrimaryGroup(DirectoryEntry aEntry, DirectoryEntry aDomainEntry)
    {
       int primaryGroupID = (int)aEntry.Properties["primaryGroupID"].Value;
       byte[] objectSid = (byte[])aEntry.Properties["objectSid"].Value;
    
       StringBuilder escapedGroupSid = new StringBuilder();
    
       // Copy over everything but the last four bytes(sub-authority)
       // Doing so gives us the RID of the domain
       for(uint i = 0; i < objectSid.Length - 4; i++)
       {
          escapedGroupSid.AppendFormat("\\{0:x2}", objectSid[i]);
       }
    
       //Add the primaryGroupID to the escape string to build the SID of the primaryGroup
       for(uint i = 0; i < 4; i++)
       {
          escapedGroupSid.AppendFormat("\\{0:x2}", (primaryGroupID & 0xFF));
          primaryGroupID >>= 8;
       }
    
       //Search the directory for a group with this SID
       DirectorySearcher searcher = new DirectorySearcher();
       if(aDomainEntry != null)
       {
           searcher.SearchRoot = aDomainEntry;
       }
    
       searcher.Filter = "(&(objectCategory=Group)(objectSID=" + escapedGroupSid.ToString() + "))";
       searcher.PropertiesToLoad.Add("distinguishedName");
    
       return searcher.FindOne().Properties["distinguishedName"][0].ToString();
    }
    

    希望这会有所帮助。

    马克

    【讨论】:

      【解决方案2】:

      用户主要组的 RID 存储在用户对象的“primaryGroupID”属性中。您必须获取给定用户(或用户其他 API)的 DirectoryEntry 才能检索此值。获得该值后,您必须将其转换为主要组的 SID,然后从中获取组。

      有一篇 KB 文章对此有更多详细信息,以及如何找到主要组的 VB 代码,此处:http://support.microsoft.com/kb/297951

      【讨论】:

        【解决方案3】:
               using (PrincipalContext context = XXX)
                {   //get the group
                        using (GroupPrincipal groupPrincipal =
                                GroupPrincipal.FindByIdentity(context,IdentityType.SamAccountName, group))
                        {
                            if (groupPrincipal != null)
                            {
                                //get the user
                                using (UserPrincipal userPrincipal =
                          UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
                                {
                                    if (userPrincipal != null)
                                    {
                                        returnValue = userPrincipal.IsMemberOf(groupPrincipal);
                                    }
                                }
                            }
                        }
        
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多