[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retrieve Active Directory Username from GUID with LDAP

Posted on 2009-04-29
8
Medium Priority
?
3,740 Views
Last Modified: 2013-12-24
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?
0
Comment
Question by:sypher1987
  • 5
  • 3
8 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24261593

> 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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24261665

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
 

Author Comment

by:sypher1987
ID: 24261770
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 71

Expert Comment

by:Chris Dent
ID: 24261783

Where's ADEntry defined?

Chris
0
 

Author Comment

by:sypher1987
ID: 24261830
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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24261857

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
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24261875

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
 

Author Closing Comment

by:sypher1987
ID: 31575982
Thanks very much, you've saved me a big headache.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question