vb.net 获取 Active Directory 中的所有用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5330743/
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
Get all users in the Active Directory
提问by StealthRT
I am trying to get a list of all users in the active directory on a domain. The following code is being used but doesn't seem to work:
我正在尝试获取域上活动目录中所有用户的列表。正在使用以下代码,但似乎不起作用:
Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable
'To retrieve list of all LDAP users
'This function returns HashTable
_ldapServerName = ldapServerName
Dim sServerName As String = "mail"
Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _
"/ou=People,dc=mydomainname,dc=com")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult
Dim RetArray As New Hashtable()
Try
oSearcher.PropertiesToLoad.Add("uid")
oSearcher.PropertiesToLoad.Add("givenname")
oSearcher.PropertiesToLoad.Add("cn")
oResults = oSearcher.FindAll
For Each oResult In oResults
If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
RetArray.Add(oResult.GetDirectoryEntry().Properties("uid").Value, _
oResult.GetDirectoryEntry().Properties("cn").Value)
End If
Next
Catch e As Exception
'MsgBox("Error is " & e.Message)
Return RetArray
End Try
Return RetArray
End Function
Just to make sure I am doing this correctly, the ldapServerName
should be the domain name that I log into that I see when I CTRL+alt+del, correct? Or would that go into the dc=mydomainname
part?
只是为了确保我正确地这样做,ldapServerName
应该是域名,我登录,我看到的时候我CTRL+ alt+ del,正确吗?或者这会进入这个dc=mydomainname
部分?
The first error in that code above is on _ldapServerName = ldapServerName
上面代码中的第一个错误是 _ldapServerName = ldapServerName
The error is says is:
错误是说:
Error 14 '_ldapServerName' is not declared. It may be inaccessible due to its protection level.
marc_s update
marc_s 更新
' create a domain context for your default domain
Dim ctx As New PrincipalContext(ContextType.Domain)
' define a "query-by-example" to search for
Dim searchExample As Principal = New UserPrincipal(ctx)
' define the principal searcher, based on that example principal
Dim ps As New PrincipalSearcher(searchExample)
' loop over all principals found by the searcher
For Each p As Principal In ps.FindAll()
' do whatever you want to do with the principals
Console.WriteLine("Type: {0} / Name: {1}", p.StructuralObjectClass, p.Name)
Next
update 2
更新 2
When I use IE and input ldap://mydomainhere.com/ou=Users
当我使用 IE 并输入时 ldap://mydomainhere.com/ou=Users
I do not get anything... But when I just do this:
我什么也没得到……但是当我这样做时:
ldap://mydomainhere.com
Then I get the "find people" box pop up. So I know I have the correct LDAP
but not sure why the other information is preventing it from working...
然后我弹出“找人”框。所以我知道我有正确LDAP
但不确定为什么其他信息阻止它工作......
回答by marc_s
If your AD isn't too big, and you're on .NET 3.5 or up (which I assume, since you're using VS2010), you should be able to write something like:
如果您的 AD 不是太大,并且您使用的是 .NET 3.5 或更高版本(我假设,因为您使用的是 VS2010),您应该能够编写如下内容:
// create a domain context for your default domain
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" to search for
Principal searchExample = new UserPrincipal(ctx);
// define the principal searcher, based on that example principal
PrincipalSearcher ps = new PrincipalSearcher(searchExample);
// loop over all principals found by the searcher
foreach(Principal p in ps.FindAll())
{
// do whatever you want to do with the principals
Console.WriteLine("Type: {0} / Name: {1}", p.StructuralObjectClass, p.Name);
}
PS:In order to "find your LDAP", you could have a look at my C#, open-source LDAP browser called BeaverTail- available for free (C#, .NET 1.1 timeframe)
PS:为了“找到您的 LDAP”,您可以查看我的 C#、开源 LDAP 浏览器BeaverTail- 免费提供(C#、.NET 1.1 时间框架)
Update:if you want to select all users in a specific location (and its sub-containers), you can do this by specifying that "starting point" in your domain context:
更新:如果您想选择特定位置(及其子容器)中的所有用户,您可以通过在域上下文中指定“起点”来执行此操作:
// create a domain context for your default domain,
// starting at a specific location
PrincipalContext ctx =
new PrincipalContext(ContextType.Domain, "YOURDOMAIN",
"OU=Personnel,OU=Users,DC=YourDomain,DC=com");
// define a "query-by-example" to search for
Principal searchExample = new UserPrincipal(ctx);
// define the principal searcher, based on that example principal
PrincipalSearcher ps = new PrincipalSearcher(searchExample);
// loop over all principals found by the searcher
foreach(Principal p in ps.FindAll())
{
UserPrincipal up = (p as UserPrincipal);
if(up != null)
{
// do whatever you want to do with the principals
Console.WriteLine("Name: {0} / E-Mail: {1}", up.Name, up.EmailAddress);
}
}
回答by Rico DAndrea
CStr(userlist(i).Properties("samAccountName").ToString)
Change to:
改成:
userlist(i).GetDirectoryEntry().Properties("givenName").Value
That change worked for me
这种改变对我有用
回答by rumbalia
In my particular instance, I had to use:
在我的特定情况下,我不得不使用:
Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://CN=Users,DC=YOUR_DOMAIN_NAME_HERE,DC=local")