DirectoryServices - Get users from active directory

Hello!
I'm using the following code to get the users of a specific active directory, but it also returns the computer names. How can I get the users only?

Dim myDirectoryAsNew DirectoryEntry("LDAP://" & directoryName, sAdminUser, sAdminPassword)
Dim mySearcherAsNew DirectorySearcher(myDirectory)
mySearcher.Filter ="(&(objectClass=user)(objectClass=person))"
Dim mySearchResultCollAs SearchResultCollection = mySearcher.FindAll()
ForEach mySearchResultAs SearchResultIn mySearchResultColl
.....
Next

[1118 byte] By [papadi] at [2007-12-16]
# 1
Anybody? Should I post this one to an other forum?
papadi at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
I don't know how to get the username returned on its own from Directory Services but this is how I did it (a while back, admittedly).

The value this function returns is [Computer Name]/[UserName], I believe... you simply do a substring with the starting position defined as (IndexOf("/") + 1).

Paul_G at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

Hi Paul,
thats not what I mean. Let me refrase: The code above returns users and computers of a domain. I want only the get the users and not the computers.

papadi at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4

As I explained in my previous answer, I didn't know how to get just the username just how to get the "[domain]/username".

However, perhaps you could try this:

DirectoryServices.DirectoryEntry entry = new DirectoryEntry([your user]);
string myUserName = entry.
Properties["UserName"].Value.ToString();

It may be what you are looking for.

Paul_G at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

You answering a completely different question! Here is what my code returns:
user1
user2
computer1
computer2
user3
computer4
user5
user6

I want to find out how it will return:
user1
user2
user3
user5
user6
i.e. without computers!!!

papadi at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6
Ah! Now I follow you..! Sorry about the misunderstanding.

I suspect the problem is in your filter. Try running it just with (&objectClass=user) and not the person one. I'll have a go in the meantime to see if I can re-produce your bug.

Paul_G at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7

I find that this works for me. I know its not the same as your code, but its the same idea.




//_

DirectoryEntry entry = new DirectoryEntry("WinNT://"+ domain, adminUser, password);

foreach(DirectoryEntry child in entry.Children)

{

if(child.SchemaClassName.ToLower(Thread.CurrentThread.CurrentUICulture).CompareTo("user")==0)

{

comboBox1.Items.Add(child.Name.ToString());

}

}

//

Paul_G at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 8
It would be interesting to know if Paul_G's suggestion works; however, I think that computer accounts can be distinguished from user accounts primarily in that computer accounts end in "$". I hope that Paul's "culture" code provides a more trustworthy differentiator.
courion24 at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 9
I think your filter is the problem try: (&(&(objectClass=user)(objectCategory=person)))
infamous at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 10
thanks infamous!!! That's it!
papadi at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 11

"(sAMAccountType=805306369)" "(sAMAccountType=805306368)"

One of those two types is computer.. the other is user. Either can be used as a filter. Who knows..

maybe you'll want Groups too, so here is the filter for all groups.

"(objectCategory=group)"

Slackshot at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified