C# 在.NET中从Active Directory组添加和删除用户
我正在编写以下方法来在C#中添加和删除active directory中的用户 如何最好地实现这些方法 下面是CodeProject中的一些代码。但是,我看不出在这些示例中指定了AD服务器的位置?(在使用LDAP协议时,它是否由.NET framework隐式提供?)。这些例子值得效仿吗C# 在.NET中从Active Directory组添加和删除用户,c#,.net,active-directory,ldap,active-directory-group,C#,.net,Active Directory,Ldap,Active Directory Group,我正在编写以下方法来在C#中添加和删除active directory中的用户 如何最好地实现这些方法 下面是CodeProject中的一些代码。但是,我看不出在这些示例中指定了AD服务器的位置?(在使用LDAP协议时,它是否由.NET framework隐式提供?)。这些例子值得效仿吗 public void AddToGroup(string userDn, string groupDn) { try { DirectoryEntry dirEntry = n
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();
}
}
服务器是组DN变量值的一部分。例如: LDAP://myServer/CN=MyGroup,CN=Groups,CN=MyContainer,DN=mydomain.com 整个过程就是组的LDAP路径。第一部分(myServer)是服务器名
服务器名称(例如CN=…)后面的部分是组的DN(可分辨名称)。您可以将LDAP服务器放在DirectoryEntry的path参数中,因此“LDAP://”+ldapServer+ldapQuery
如果需要进行身份验证,请使用DirectoryEntry(字符串路径、字符串用户ID、字符串密码)。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();
}
}
在中删除成员时
public void RemoveUserFromGroup(字符串userDn,字符串groupDn)
dirEntry.Properties[“member”].Remove(userDn)
对我不起作用
dirEntry.Properties[“member”].RemoveAt(dn.IndexOf(dn))
有效。我唯一想说的是,在一个好的广告设置中,您不必指定服务器。.NET AD/低级AD调用应为您解析最近的可用服务器。但这更多的是AD/域设置,而不是太多的代码。如果您的广告设置是可靠的,您应该能够排除服务器(例如LDAP://CN=MyGroup,CN=Groups,CN=MyContainer,DN=mydomain.com)。很抱歉,您没有真正回答您的问题。是的,这些例子看起来很清楚。如果您仍然不确定,我强烈推荐.NET开发人员目录服务编程指南()System.DirectorServices.AccountManagement仅在>=3.5版本中提供,而不是在下面的3.0版本中为我工作的代码group.Members.Remove(UserPrincipal.FindByIdentity(pc,userId));而不是“group.Members.Remove(pc,IdentityType.UserPrincipalName,userId);”。注意:我的用户id只是“USERNAME”,没有附加域名,与上述问题类似。我必须将从组中删除用户的行从IdentityType.UserPrincipalName更改为IdentityType.SamAccountName什么是用户ID?我知道上面的评论中提到了这一点,但没有澄清。如果您以用户身份发送DOMAIN\someUserId
,则必须将其更改为IdentityType.SamAccountName
,而不是UserPrincipalName
。后者将给您一个异常:未找到与指定参数匹配的主体
。但也请注意,如果用户已经在组中,它也会给您一个异常,即SamAccountName
。dn
变量是什么?@fripp13,在Active Directory的上下文中,dn几乎总是指DiscrimitedName。
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();
}
}