C# 在 .NET 的 Active Directory 组中添加和删除用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2143052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding and removing users from Active Directory groups in .NET
提问by Ben Aston
I am writing the following methods to add and remove users from active directory in C#.
我正在编写以下方法来在 C# 中的活动目录中添加和删除用户。
void AddUserToGroup(string userId, string groupName);
void RemoveUserFromGroup(string userId, string groupName);
How best to implement these methods?
如何最好地实现这些方法?
Here is some code from CodeProject. I can't see where the AD server is specified in these examples though? (is it implicitly supplied by the .NET framework when using the LDAP protocol?). Are these examples worth following?
这是来自 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();
}
}
采纳答案by Jacob Proffitt
Ugh. LDAP. If you're using the .Net Framework 3.5 or above, I highly recommend using the System.DirectoryServices.AccountManagement namespace. That makes things somuch easier.
啊。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();
}
}
回答by Mike Marshall
The server is part of the groupDnvariable value. For example:
服务器是groupDn变量值的一部分。例如:
LDAP://myServer/CN=MyGroup,CN=Groups,CN=MyContainer,DN=mydomain.com
LDAP://myServer/CN=MyGroup,CN=Groups,CN=MyContainer,DN=mydomain.com
The whole thing is the LDAP path for the group. The first part (myServer) is the server name.
整个事情就是组的 LDAP 路径。第一部分 (myServer) 是服务器名称。
The part after the server name (e.g. CN=...) is the DN (distinguished name) of the group.
服务器名称(例如 CN=...)后面的部分是组的 DN(专有名称)。
回答by Mason
You can put the LDAP server in the path argument to DirectoryEntry, so "LDAP://" + ldapServer + ldapQuery.
您可以将 LDAP 服务器放在 DirectoryEntry 的路径参数中,因此“LDAP://”+ ldapServer + ldapQuery。
Use the DirectoryEntry(String path, String userId, String password) if you need to authenticate
如果需要进行身份验证,请使用 DirectoryEntry(String path, String userId, String password)
回答by Andy
When deleting a member in
public void RemoveUserFromGroup(string userDn, string groupDn)
删除成员时
public void RemoveUserFromGroup(string userDn, string groupDn)
dirEntry.Properties["member"].Remove(userDn)
does not work for me.
dirEntry.Properties["member"].Remove(userDn)
对我不起作用。
dirEntry.Properties["member"].RemoveAt(dn.IndexOf(dn))
works.
dirEntry.Properties["member"].RemoveAt(dn.IndexOf(dn))
作品。