Otuyh
Otuyh

Reputation: 2484

Can i get a specific user by some attribute without knowing the OU's in Active Directory (AD)?

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

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

OU=ThirdParty
    OU=Company1
        CN=User1   
        CN=User2 
        CN=User3 
    OU=Company2
        CN=User1 
        CN=User2 
        CN=User3 

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();
try
{
    SearchResultCollection result = search.FindAll();
    .....
}

Thanks!

EDIT:

Ok, i got it using this code:

DirectorySearcher search = new DirectorySearcher(_entry, "(sAMAccountName=" + userCode + ")");

Upvotes: 2

Views: 273

Answers (3)

pistipanko
pistipanko

Reputation: 775

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) {
        info.Add(foundUP.SamAccountName.ToLower);
        info.Add(foundUP.GivenName);
        info.Add(foundUP.Surname);
        info.Add(foundUP.EmailAddress.ToLower);
        return info;
    }

    return null;
}

Upvotes: 0

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

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")

Upvotes: 1

Anthony Shaw
Anthony Shaw

Reputation: 8166

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

Upvotes: 1

Related Questions