【问题标题】:Adding and removing users from Active Directory groups in .NET在 .NET 中的 Active Directory 组中添加和删除用户
【发布时间】:2011-01-09 17:26:13
【问题描述】:

我正在编写以下方法来在 C# 中从活动目录中添加和删除用户。

void AddUserToGroup(string userId, string groupName);
void RemoveUserFromGroup(string userId, string groupName);

如何最好地实现这些方法?

这是来自 CodeProject 的一些代码。我看不到在这些示例中指定 AD 服务器的位置吗? (在使用 LDAP 协议时,它是由 .NET 框架隐式提供的吗?)。这些例子值得学习吗?

public void AddToGroup(string userDn, string groupDn)
{
    try
    {
        DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
        dirEntry.Properties["member"].Add(userDn);
        dirEntry.CommitChanges();
        dirEntry.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //doSomething with E.Message.ToString();

    }
}


public void RemoveUserFromGroup(string userDn, string groupDn)
{
    try
    {
        DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
        dirEntry.Properties["member"].Remove(userDn);
        dirEntry.CommitChanges();
        dirEntry.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //doSomething with E.Message.ToString();

    }
}

【问题讨论】:

    标签: c# .net active-directory ldap active-directory-group


    【解决方案1】:

    呃。 LDAP。如果您使用的是 .Net Framework 3.5 或更高版本,我强烈建议您使用 System.DirectoryServices.AccountManagement 命名空间。这让事情如此变得容易多了。

    public void AddUserToGroup(string userId, string groupName) 
    { 
        try 
        { 
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "COMPANY"))
            {
                GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
                group.Members.Add(pc, IdentityType.UserPrincipalName, userId);
                group.Save();
            }
        } 
        catch (System.DirectoryServices.DirectoryServicesCOMException E) 
        { 
            //doSomething with E.Message.ToString(); 
    
        } 
    } 
    
    public void RemoveUserFromGroup(string userId, string groupName)
    {   
        try 
        { 
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "COMPANY"))
            {
                GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
                group.Members.Remove(pc, IdentityType.UserPrincipalName, userId);
                group.Save();
            }
        } 
        catch (System.DirectoryServices.DirectoryServicesCOMException E) 
        { 
            //doSomething with E.Message.ToString(); 
    
        }
    }
    

    【讨论】:

    • System.DirectorServices.AccountManagement 仅适用于 >= 3.5,而不是 3.0
    • 下面的代码对我有用 group.Members.Remove(UserPrincipal.FindByIdentity(pc, userId)); 而不是 "group.Members.Remove(pc, IdentityType.UserPrincipalName, userId);" .注意:我的用户名只是“用户名”,没有附加域名
    • 遇到了与上述类似的问题。我不得不将从组中删除用户的行从 IdentityType.UserPrincipalName 更改为 IdentityType.SAMAccountName
    • 什么是 userId
    • 我知道在上面的cmets中说过,但没有澄清。如果您以用户身份发送DOMAIN\someUserId,则必须将其更改为IdentityType.SamAccountName,而不是UserPrincipalName。后者会给你一个例外:No principal matching the specified parameters was found。但也要注意,如果用户已经在组中,它也会给你一个 SamAccountName 的例外。
    【解决方案2】:

    删除成员时 public void RemoveUserFromGroup(string userDn, string groupDn)

    dirEntry.Properties["member"].Remove(userDn) 不适合我。

    dirEntry.Properties["member"].RemoveAt(dn.IndexOf(dn)) 有效。

    【讨论】:

    • dn 变量是什么?
    • @fripp13,在 Active Directory 的上下文中,dn 几乎总是意味着 DistinguishedName。
    【解决方案3】:

    服务器是 groupDn 变量值的一部分。例如:

    LDAP://myServer/CN=MyGroup,CN=Groups,CN=MyContainer,DN=mydomain.com

    整个就是组的 LDAP 路径。第一部分 (myServer) 是服务器名称。

    服务器名称后的部分(例如 CN=...)是组的 DN(可分辨名称)。

    【讨论】:

    • 我唯一想说的是,在良好的 AD 设置中,您不必指定服务器。 .NET AD/低级 AD 调用应该为您解析最近的可用服务器。但这更多的是 AD/域设置,而不是太多代码。如果您的 AD 设置可靠,您应该能够排除服务器(例如 LDAP://CN=MyGroup,CN=Groups,CN=MyContainer,DN=mydomain.com)
    • 很抱歉没有真正回答您的问题。是的,这些例子看起来确实很干净。如果您仍然不确定,我强烈推荐 .NET Developer's Guide to Directory Services Programming (amazon.com/gp/product/0321350170)
    【解决方案4】:

    您可以将 LDAP 服务器放在 DirectoryEntry 的路径参数中,因此 "LDAP://" + ldapServer + ldapQuery。

    如果您需要进行身份验证,请使用 DirectoryEntry(String path, String userId, String password)

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多