[英]How to get users from Active Directory in C# and display them in a ComboBox
I'm trying to show, in a ComboBox
control, the users from an Active Directory on the network.我试图在ComboBox
控件中显示来自网络上 Active Directory 的用户。 To do this, I've the next function:为此,我有下一个功能:
public static List<Usuario> MostrarUsuariosDominio()
{
List<Usuario> rst = new List<Usuario>();
try
{
DirectoryContext dc = new DirectoryContext(DirectoryContextType.Domain, Environment.UserDomainName);
Domain domain = Domain.GetDomain(dc);
DirectoryEntry de = domain.GetDirectoryEntry();
DirectorySearcher adSearcher = new DirectorySearcher(de);
adSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";
adSearcher.PropertiesToLoad.Add("samaccountname");
SearchResult result;
SearchResultCollection iResult = adSearcher.FindAll();
Usuario item;
if (iResult != null)
{
for (int counter = 0; counter < iResult.Count; counter++)
{
result = iResult[counter];
if (result.Properties.Contains("samaccountname"))
{
item = new Usuario();
item.Nombre = (String)result.Properties["samaccountname"][0];
rst.Add(item);
}
}
}
adSearcher.Dispose();
}
catch (Exception ex)
{
Usuario item = new Usuario();
item.Nombre = "No se pudo recuperar la lista de usuarios";
rst.Add(item);
}
return rst;
}
If I run the application in the PC who's domain controller it works fine: the function returns to me all users.如果我在域控制器的 PC 上运行应用程序,它工作正常:该函数将所有用户返回给我。 But if I run it on another PC, I get the exception:但如果我在另一台 PC 上运行它,我会遇到异常:
Specified domain does not exist or couldn't contact with it指定的域不存在或无法与其联系
Is there any way to recover the users list from another PC?有什么方法可以从另一台 PC 恢复用户列表?
This line:这一行:
DirectoryContext dc = new DirectoryContext(DirectoryContextType.Domain, Environment.UserDomainName);
Tells it to connect to the domain that the current user is logged into.告诉它连接到当前用户登录的域。 Are you logged in as a domain user?您是否以域用户身份登录?
Maybe check what Environment.UserDomainName
is equal to and see if it is right.也许检查Environment.UserDomainName
等于什么,看看它是否正确。
If it's right, then it may be a network issue - it can't talk to the domain.如果它是正确的,那么它可能是一个网络问题 - 它无法与域对话。 Do you need to be connected to VPN?你需要连接到VPN吗?
To get all the users in a Active Directory domain you can use an DirectorySearcher
class object to querie to a domain about all the users availables in that domain.要获取 Active Directory 域中的所有用户,您可以使用DirectorySearcher
类对象向域查询有关该域中所有可用用户的信息。
The class DirectorySearcher
is contained in System.DirectoryServices
namespace and is a class to perform queries against Active Directory Domain Services. DirectorySearcher
类包含在System.DirectoryServices
命名空间中,是用于对 Active Directory 域服务执行查询的类。
In this page is an example about how to do it:在此页面中是有关如何执行此操作的示例:
... string DomainPath = "LDAP://DC=xxxx,DC=com" DirectoryEntry searchRoot = new DirectoryEntry(DomainPath); DirectorySearcher search = new DirectorySearcher(searchRoot); search.Filter = "(&(objectClass=user)(objectCategory=person))"; search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("mail"); search.PropertiesToLoad.Add("usergroup"); search.PropertiesToLoad.Add("displayname");//first name SearchResultCollection resultCol = search.FindAll(); ...
In DirectorySearcher, create a DirectorySearcher object which searches for all users in a domain.在 DirectorySearcher 中,创建一个 DirectorySearcher 对象,用于搜索域中的所有用户。
search.Filter = "(&(objectClass=user)(objectCategory=person))"
filters the search.search.Filter = "(&(objectClass=user)(objectCategory=person))"
过滤搜索。The search filter syntax looks a bit complicated, but basically it filters the search results to only include users ->
"objectCategory=person"
and"objectClass=user"
- and excludes disabled user accounts by performing a bitwise AND of the userAccountControl flags and the "account disabled" flag.搜索过滤器语法看起来有点复杂,但基本上它将搜索结果过滤为仅包含用户 ->"objectCategory=person"
和"objectClass=user"
- 并通过对 userAccountControl 标志和“帐户禁用”标志。
To point to the local Active Directory domain you can use this:要指向本地 Active Directory 域,您可以使用以下命令:
DirectoryEntry searchRoot = new DirectoryEntry("WinNT://" + Environment.MachineName);
You can combine that example with this code that uses foreach
instead of use the for
loop in the page example:您可以将该示例与使用foreach
的代码结合起来,而不是在页面示例中使用for
循环:
foreach (SearchResult result in resultCol)
{
yourComboBox.Items.Add(result.Properties["displayname"]);
}
I left here some pages from the Microsoft MSDN and codeproject.com sites:我在这里留下了一些来自 Microsoft MSDN 和 codeproject.com 站点的页面:
DirectorySearcher Class DirectorySearcher 类
Get List of Active Directory Users in C# 在 C# 中获取 Active Directory 用户列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.