Take the 2-minute tour ×

I need to get a specific user only knowing the "sAMAAccountName" field.

The thing is, this specific user can be inside of many groups:


Is there any way to get an user not knowing their groups, only using one attribute that they have?

My code:

DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(&(objectCategory=person)(objectClass=User))";
StringBuilder groupNames = new StringBuilder();
    SearchResultCollection result = search.FindAll();



Ok, i got it using this code:

DirectorySearcher search = new DirectorySearcher(_entry, "(sAMAccountName=" + userCode + ")");
share|improve this question
In Anthony Shaw link, have an example doing exatcly the same thing i want! Thanks! –  Otuyh Feb 23 '12 at 20:46

3 Answers 3

up vote 1 down vote accepted

What information do you need to know about the user? We have used this type of code in the past to retrieve information about a user

using (var identity = new WindowsIdentity(username))
    var user = new WindowsPrincipal(identity);

    if (user.IsInRole("Some Role Name"))
        return true;

    return false;

EDIT After your comment, I wonder if this post would provide you any further insite. They so show getting the field you're requesting, I'm just not sure the code to retrieve the employee will apply to you since this refers to InfoPath: http://msdn.microsoft.com/en-us/library/bb952744(v=office.12).aspx

share|improve this answer
Well, i need to know the "physicalDeliveryOfficeName" field, dont know if this is relevant. I'll try ur code! Thanks! –  Otuyh Feb 23 '12 at 19:54
added a link to an msdn article regarding infopath where it looks like they're grabbing the physicalDeliveryOfficeName, you might give it a look as well –  Anthony Shaw Feb 23 '12 at 20:06
Thanks! This link have the answer! –  Otuyh Feb 23 '12 at 20:46
great, glad I could help –  Anthony Shaw Feb 23 '12 at 20:50

If you switch to System.DirectoryServices.AccountManagement then you'll find out that the APIs are in fact much simpler.

For example:

    public something FindUserByUserName( string UserName )
        using ( var searcher = 
            new PrincipalSearcher( new UserPrincipal( ConfigurationContext ) { Name = UserName } ) )
            var item = searcher.FindOne();

            // do whatever you want with the found object and return it

where ConfigurationContext is a property which returns the PrincipalContext (credentials to connect to the AD, something like the "connection string")

share|improve this answer

Try this:

public static List<string> GetADUserInfo(string login)
    //Using Hosting.HostingEnvironment.Impersonate()
    List<string> info = new List<string>();
    PrincipalContext infPC = new PrincipalContext(ContextType.Domain, "domain", "login", "password");
    UserPrincipal infUP = new UserPrincipal(infPC);
    PrincipalSearcher infPS = new PrincipalSearcher();
    UserPrincipal foundUP;

    infUP.SamAccountName = login;
    infPS.QueryFilter = infUP;
    foundUP = infPS.FindOne();

    if (foundUP != null) {
        return info;

    return null;
share|improve this answer

Your Answer


