How to get ou of an user

G

Guest

Hi
I wonder if there is other / faster ways to get ou of the login user?

Here is what I can think of
- authentication
- Search with SAMAccountName=<username entered>
- set PropertiesToLoad.Add("distinguishedName")
- parse the OU= string from the property returned

TIA

--
 
J

Joe Kaplan \(MVP - ADSI\)

That will probably work fine. You might also consider p/invoking the
TranslateName Windows API as it can convert from login name to distinguished
name.

If you do parse, be careful about encoded "," characters in the DN. You
can't be 100% sure that a simple Split on the comma will work as the name
component can have a "," in it if it is preceded by "\". Thus you could
have:

CN=Doe\, Jane,OU=people,DC=domain,DC=com

If your AD doesn't use that naming convention, you are fine, but just
remember that it is legal syntax and could cause funny bugs at runtime in
code that seems to work fine 95% of the time.

Joe K.
 
J

Jan Peter Stotz

dl said:
Here is what I can think of
- authentication
- Search with SAMAccountName=<username entered>
- set PropertiesToLoad.Add("distinguishedName")
- parse the OU= string from the property returned

Why parsing the dn when you can just ask the corresponding class?

If you search the account by using the DirectorySearcher class, as result
you will get an object of type SearchResult. Call on this object
..GetDirectoryEntry().Parent.Path and you get what you want.

Jan
 
G

Guest

Thanks Joe.

Joe Kaplan (MVP - ADSI) said:
That will probably work fine. You might also consider p/invoking the
TranslateName Windows API as it can convert from login name to distinguished
name.

If you do parse, be careful about encoded "," characters in the DN. You
can't be 100% sure that a simple Split on the comma will work as the name
component can have a "," in it if it is preceded by "\". Thus you could
have:

CN=Doe\, Jane,OU=people,DC=domain,DC=com

If your AD doesn't use that naming convention, you are fine, but just
remember that it is legal syntax and could cause funny bugs at runtime in
code that seems to work fine 95% of the time.

Joe K.
 
G

Guest

Thanks Jan. But what is returned by the GetDirectoryEntry().Parent.Path?
can you give a sample format please?
TIA
 
J

Jan Peter Stotz

dl said:
Thanks Jan. But what is returned by the GetDirectoryEntry().Parent.Path?
can you give a sample format please?

The "DirectoryEntry.Path Property" is a String property. For a detailed
description search the .Net Framwork SDK Documentation.

VB.NET:

Dim searcher as new DirectorySearcher("LDAP://dc=yourdomain,dc=com")
searcher.Filter="(&(objectClass=user)(samaccountname=" & username & "))"
Dim result as SearchResult = searcher.FindOne
Dim userEntry as DirectoryEntry = result.GetDirectoryEntry()
Dim containerEntry as DirectoryEntry = userEntry.Parent
Dim String containerPath = containerEntry.Path

Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top