Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActiveDirectory: How to get user name. 1

Status
Not open for further replies.

buraje1

Technical User
Mar 13, 2005
7
DE
Hello folks,
I've been trying to find out for a while,
why I am not getting the user name, when I access the active directory under .NET.

I am using this simple code:
Code:
DirectoryEntry entry = new DirectoryEntry("LDAP://OU=User organisation, ...");

DirectorySearcher mySearcher = new DirectorySearcher(entry);

SearchResultCollection result = mySearcher.FindAll();

foreach (SearchResult s in result)
{
    DirectoryEntry de = s.GetDirectoryEntry();

    Console.WriteLine("Name:\t" + de.Name);
    [COLOR=red]Console.WriteLine("Username:\t" + de.Username);[/color]
    
    PropertyCollection p = de.Properties;

    if (p["objectSid"].Value == null)
        Console.WriteLine("No SID available");
    else
        Console.WriteLine("SID:\t" + 
            new SecurityIdentifier((byte []) p["objectSid"].Value, 0).ToString());

    [COLOR=red]if (p["username"].Value == null)
        Console.WriteLine("No username available");[/color]
    else
        Console.WriteLine("Username:\t" + (string) p["username"].Value);

    Console.WriteLine("################################################\n");
}

Console.WriteLine("Why am I not getting any user names?");

This code yields:

Code:
Name:   OU=User organisation
[COLOR=red]Username:[/color] [b]//that should be normal[/b]
No SID available [b]//that should be normal[/b]
No username available [b]//that should be normal[/b]
################################################

Name:   CN=Steven
[COLOR=red]Username:[/color] [b]//why is my user name not shown?[/b]
SID:    S-1-5-21-3592736010-1634432720-1998580803-xxxxx
No username available [b]//so the value is null! Why?[/b]
################################################

Name:   CN=Fantasy U. Ser
[COLOR=red]Username:[/color] [b]//why is 'fantasiy's user name not shown?[/b]
SID:    S-1-5-21-3592736010-1634432720-1998580803-yyyyy
No username available [b]//so the value is null! Why?[/b]
################################################

Name:   CN=Another User
Username:
SID:    S-1-5-21-3592736010-1634432720-1998580803-zzzzz
No username available
################################################

Why am I not getting any user names?

And yes, that's the question. Why am I not getting any user names? Why is
Code:
 de.Username
always
Code:
 null
?
How do I get them? I need both the SID and the user name for my app...

Thanks for your help,

Steven
 
it's been awhile since I used DirectoryServices object, but what I remember is you need to tell the DirectorySearcher which properties to load. then you need to FindOne/FindAll. then you need to get the value from the property loaded. I remember it looking something like this
Code:
var searcher = new DirectorySearcher();
search.PropertiesToLoad(new[]{"samaccount","displayname","mail"});
var result = searcher.FindOne();
var username = result.Properties["samaccountname"][0];
var fullName = result.Properties["displayname"][0];
var emailAddress = result.Properties["mail"][0];

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks, Jason. That did the trick.
For others reading this post later:
"PropertiesToLoad" is a read-only property, so you can't assign a value to it, that's why Jason's solution requires a little bit of research, but still solves the problem.
Instead, you'll have to give that string array to the constructor of your DirectorySearcher.
Then you'll find out that you have hand the constructor a filter string, because the constructor overload that takes the PropertiesToLoad-array requires this.
If you're like me wondering what a filter string may look like, don't try the empty string "": not a lucky shot, it throws an Exception and says sth about illegal filter.

The filter "(objectClass=*)" is not a filter as such, because doesn't filter anything, which is exactly what I wanted for now.
Read about the Filter and the PropertiesToLoad here:

This is the code that solved my problem:
Code:
DirectorySearcher dirSearcher = new DirectorySearcher(
    new DirectoryEntry("LDAP://OU=User organisation, ..."),
    "(objectClass=*)",
    new string[] { "sAMAccountName", "displayname", "mail" });

// which properties? => Exactly the ones I handed over to the constructor. 
// So, Anders H. and Scott Gu., why is this property read-only?

foreach(string s in dirSearcher.PropertiesToLoad)
    Console.WriteLine("PropertyToLoad:\t" + s);

foreach (SearchResult s in dirSearcher.FindAll())
{
    PropertyCollection p = s.GetDirectoryEntry().Properties;
    
    if (p["objectSid"].Value == null)
        Console.WriteLine("No SID available");
    else
        Console.WriteLine("SID:\t" + 
            new SecurityIdentifier((byte []) p["objectSid"].Value, 0).ToString());

    if (p["sAMAccountName"].Value == null)
        Console.WriteLine("No username available");
    else
        Console.WriteLine("Username:\t" + (string)p["sAMAccountName"].Value);
        
    if (p["displayname"].Value == null)
        Console.WriteLine("No display name available");
    else
        Console.WriteLine("Display name:\t" + (string)p["displayname"].Value);
        
Console.WriteLine("################################################");
}

Yields

Code:
PropertyToLoad: sAMAccountName
PropertyToLoad: displayname
PropertyToLoad: mail

No SID available
No username available
No display name available
################################################
SID:    S-1-5-21-3592736010-1634432720-1998580803-xxxxx
Username:       steven
Display name:   Steven
################################################
SID:    S-1-5-21-3592736010-1634432720-1998580803-yyyyy
Username:       fantasy
Display name:   Fantasy U. Ser
################################################
SID:    S-1-5-21-3592736010-1634432720-1998580803-zzzzz
Username:       another
Display name:   Another User
################################################
 
The filter "(objectClass=*)" is not a filter as such, because doesn't filter anything, which is exactly what I wanted for now.

Bulls... erm. A little self correction:
of course what I want is the filter "(objectClass=user)". :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top