Can someone please tell me how to get a listing of users and their user account numbers (in string type, or easily convertible to string type) from active directory? Sample code would be nice. I've googled this already with no success; lot's of code that
claims to do it, none that I have been able to get to work.
With this code, you can search an active directory create a list of usernames, if you need more propertys,build a struct, and fill a list with this structs.
DirectoryEntry domain1;
DirectorySearcher searcher1;
List<string> lista1 = new List<string>();
dominio = new DirectoryEntry("LDAP://test.com/DC=dept1,DC=com", "domainadmin", "12345", AuthenticationTypes.ReadonlyServer);
searcher1= new DirectorySearcher("(&(objectCategory=Person)(objectClass=user)");
searcher1.SearchRoot = domain1;
searcher1.SearchScope = SearchScope.Subtree;
SearchResultCollection results1;
results1= searcher1.FindAll();
for (int i = 0; i < results1.Count; i++)
{
lista1 .Add(results1[i].Properties["samaccountname"][0].ToString());
}
yes, the green part is the connection string to ldap or active directory that you will use.
You need the active directory server and OU of your domain, a user and a password, minimum with readonly, a common user it's enought to extract the user list.
the list<string> in c# to vb.net :
Dim stringlist As New System.Collections.Generic.List(Of String)()
where the <string> translate like (Of String), because is a generic list.
I translated it to vb like this (you have an item called "dominio" in your c# that I think you meant to be domain1?). My questions follow:
Dim Domain1 As DirectoryEntry =
New DirectoryEntry("LDAP://test.com/DC=dept1,DC=com",
"domainadmin",
"12345", AuthenticationTypes.ReadonlyServer)
Dim Searcher1
As DirectorySearcher =
New DirectorySearcher("(&(objectCategory=Person)(objectClass=user)")
Dim Lista1 As
New System.Collections.Generic.List(Of
String)()
Dim Results1 As
New SearchResultCollection
Results1 = Searcher1.FindAll()
For i As
Integer = 0 To Results1.Count
Lista1.Add(Results1(i).Properties("samaccountname")(0).ToString())
Next
I have these questions, if you don't mind:
1. VS is complaining about the Dim Results1 statement with a blue squiggly under Results1 that says that "Type 'System.DirectoryServices.SearchResultCollection' has no constructors". I've tried rearranging
that statement in every possible syntax I can think of, but that hasn't helped. Do you know what the problem is?
2. Do you know how I can find out what the "active directory server and ou of my domain" is? And how do I arrange that in the code?
3. What account do I put in for "somaccountname"? How can find a list of eligible accounts?
Never mind my first question about the Dim Results1 statement. I fixed that. But if you could please answer questions 2 and 3, that would help. thanks again.
in this case, the samAccountname is the short name o domain user that you are searching, if you need for example, searching all that star with the letter B, you can define a filter in the searcher:
I am trying to traverse through a active directory group in classic asp.
example
obj1 = getobject("LDAP://CN=name,DC=,DC=com")
Dim memName = "test"
for each memebr in obj1.members
if (memName = obj1.name) then
flag = 1
else
flag = 0
end if
next
it doesn't work. I am trying ti compare the members of "obj1" group to "test". I am not sure what property I need to use to retrieve the member name from an active directory group.
Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://domainname") '"LDAP://domainname") '"WinNT://" & a(0) & "/" & a(1))
Dim adsUser As DirectoryEntry
Dim i As Int32 = 0
For Each adsUser In ADEntry.Children
If adsUser.SchemaClassName = "User" Then
ListBox1.Items.Add(New ListItem(adsUser.Properties("FullName").Value.ToString, adsUser.Name.ToString))
End If
Next
DesertDude
Participant
1315 Points
334 Posts
List of users from Active Directory?
Oct 14, 2008 07:40 PM|LINK
Can someone please tell me how to get a listing of users and their user account numbers (in string type, or easily convertible to string type) from active directory? Sample code would be nice. I've googled this already with no success; lot's of code that claims to do it, none that I have been able to get to work.
Thanks!
darkzen
Member
81 Points
12 Posts
Re: List of users from Active Directory?
Oct 14, 2008 08:02 PM|LINK
With this code, you can search an active directory create a list of usernames, if you need more propertys,build a struct, and fill a list with this structs.
DirectoryEntry domain1; DirectorySearcher searcher1; List<string> lista1 = new List<string>(); dominio = new DirectoryEntry("LDAP://test.com/DC=dept1,DC=com", "domainadmin", "12345", AuthenticationTypes.ReadonlyServer); searcher1= new DirectorySearcher("(&(objectCategory=Person)(objectClass=user)"); searcher1.SearchRoot = domain1; searcher1.SearchScope = SearchScope.Subtree; SearchResultCollection results1; results1= searcher1.FindAll(); for (int i = 0; i < results1.Count; i++) { lista1 .Add(results1[i].Properties["samaccountname"][0].ToString()); }DesertDude
Participant
1315 Points
334 Posts
Re: List of users from Active Directory?
Oct 15, 2008 12:44 AM|LINK
Thank you! Any chance you might have this in vb.net? I can translate most of it, but I don't know how to translate the <string> thing.
Also, how would I find out what to put in the green part? I take it that that part needs to be customized to my AD environment?
darkzen
Member
81 Points
12 Posts
Re: List of users from Active Directory?
Oct 15, 2008 06:10 PM|LINK
yes, the green part is the connection string to ldap or active directory that you will use.
You need the active directory server and OU of your domain, a user and a password, minimum with readonly, a common user it's enought to extract the user list.
the list<string> in c# to vb.net :
Dim stringlist As New System.Collections.Generic.List(Of String)()
where the <string> translate like (Of String), because is a generic list.
I hope this can help you .
DesertDude
Participant
1315 Points
334 Posts
Re: List of users from Active Directory?
Oct 21, 2008 10:08 PM|LINK
Thank you for your continued help!
I translated it to vb like this (you have an item called "dominio" in your c# that I think you meant to be domain1?). My questions follow:
Dim Domain1 As DirectoryEntry = New DirectoryEntry("LDAP://test.com/DC=dept1,DC=com", "domainadmin", "12345", AuthenticationTypes.ReadonlyServer)Dim Searcher1 As DirectorySearcher = New DirectorySearcher("(&(objectCategory=Person)(objectClass=user)")
Dim Lista1 As New System.Collections.Generic.List(Of String)()
Searcher1.SearchRoot = Domain1
Dim Results1 As New SearchResultCollectionSearcher1.SearchScope = SearchScope.Subtree
Results1 = Searcher1.FindAll() For i As Integer = 0 To Results1.Count
Lista1.Add(Results1(i).Properties("samaccountname")(0).ToString())
Next
I have these questions, if you don't mind:
1. VS is complaining about the Dim Results1 statement with a blue squiggly under Results1 that says that "Type 'System.DirectoryServices.SearchResultCollection' has no constructors". I've tried rearranging that statement in every possible syntax I can think of, but that hasn't helped. Do you know what the problem is?
2. Do you know how I can find out what the "active directory server and ou of my domain" is? And how do I arrange that in the code?
3. What account do I put in for "somaccountname"? How can find a list of eligible accounts?
Thanks again.
DesertDude
Participant
1315 Points
334 Posts
Re: List of users from Active Directory?
Oct 21, 2008 10:20 PM|LINK
Never mind my first question about the Dim Results1 statement. I fixed that. But if you could please answer questions 2 and 3, that would help. thanks again.
darkzen
Member
81 Points
12 Posts
Re: List of users from Active Directory?
Oct 22, 2008 05:49 PM|LINK
if you machine or server are already using active directory, then you can use directly without using a connection string like this:
DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher("(samaccountname=johndoe)"); SearchResult result=searcher.FindOne(); DirectoryEntry dentry= result.GetDirectoryEntry(); string fullname=dentry.Properties["displayName"].Value.ToString();searcher.Filter = ("(&(objectCategory=person)(objectClass=user)(sAMAccountName=B*"))");There area a lot of properties in the activedirectory for example:
TelephoneNumber,IPphone,Mobile ,Department,manager,employeeId,displayName,Name,givenName
But, you need to check with your active directory admin, in other way, you can extract the properties names with this code snippet:
DesertDude
Participant
1315 Points
334 Posts
Re: List of users from Active Directory?
Oct 23, 2008 10:43 PM|LINK
thank you so much for your help.
niru1234
Member
2 Points
13 Posts
Re: List of users from Active Directory?
Oct 30, 2008 03:55 AM|LINK
I am trying to traverse through a active directory group in classic asp.
example
obj1 = getobject("LDAP://CN=name,DC=,DC=com")
Dim memName = "test"
for each memebr in obj1.members
if (memName = obj1.name) then
flag = 1
else
flag = 0
end if
next
it doesn't work. I am trying ti compare the members of "obj1" group to "test". I am not sure what property I need to use to retrieve the member name from an active directory group.
Please help me.
Thanks,
Niru
roma_victa
Member
62 Points
108 Posts
Re: List of users from Active Directory?
Jul 01, 2010 12:50 PM|LINK
try this
Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://domainname") '"LDAP://domainname") '"WinNT://" & a(0) & "/" & a(1)) Dim adsUser As DirectoryEntry Dim i As Int32 = 0 For Each adsUser In ADEntry.Children If adsUser.SchemaClassName = "User" Then ListBox1.Items.Add(New ListItem(adsUser.Properties("FullName").Value.ToString, adsUser.Name.ToString)) End If Next