Retrieve Active Directory Username from GUID with LDAP

I have a piece of code in a web aplication to query active directory using LDAP and return a DirectoryEntry object based on the users GUID.

The reason i am doing it is to get the users username based on thier AD account but the username is always coming back as null even though the rest of the DirectoryEntry is correct. Does anyone know why this is?
sypher1987Asked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

Heh okay I know why now.

"You can set the Username and Password in order to specify alternate credentials with which to access the information in Active Directory Domain Services. Any other DirectoryEntry objects retrieved from this instance (for example, through Children) are automatically created with the same alternate credentials. "

From:

http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.username.aspx

You're not using alternate credentials so there's no value there.

You should find that this one works:

String username = DirEntry.Properties["sAMAccountName"].Value.ToString()

To retrieve the user name value from the account.

Chris
0
 
Chris DentPowerShell DeveloperCommented:

> The reason i am doing it is to get the users username based on thier AD account but the username
> is always coming back as null even though the rest of the DirectoryEntry is correct.

Okay silly question, but which field do you think the username is?

Chris
0
 
Chris DentPowerShell DeveloperCommented:

Alternatively, if you could share the portion of code that retrieves the username.

I would expect something like this if you have a DirectoryEntry.

String username = DirEntry.Properties["sAMAccountName"].Value.ToString()

That changes if you have a SearchResult though.

Chris
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
sypher1987Author Commented:
This is the code ive been using.
DirectoryEntry newDirectoryUser = new DirectoryEntry("LDAP://<GUID=" + userKey + ">");
 
if (UserValid(ADEntry.Guid) && !UserExists(ADEntry.Username))
{
//do stuff
}

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

Where's ADEntry defined?

Chris
0
 
sypher1987Author Commented:
Sorry i copied the code from two different places, the second part is in a method that gets the directory entry passed as ADEntry, the other properties like the name and guid are there its just the Username property thats null.
0
 
Chris DentPowerShell DeveloperCommented:

Cool, got it. I get a blank return on that property as well. Having a look into why, MSDN is a bit vague on that one :)

Chris
0
 
sypher1987Author Commented:
Thanks very much, you've saved me a big headache.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.