August 27, 2012

How to search Active directory for email and name of current NT user

You can search for your organisation's Active directory for particular user's email-id, first name, last name and many other vital information using LDAP.

what you need
1) LDAP Address
2) Any valid active directory credentials (e.g yours own)

No need to install any third party software etc.

Code snippet:-

var userWithoutDomainName = "skumar"

var entry = new DirectoryEntry("LDAP://DC=company, DC=com"/>, "username", "password");//username //is without domain
            var searcher = new DirectorySearcher{
                SearchRoot = entry,
                Filter = "(SAMAccountName=" +userWithoutDomainName + ")" };
            searcher.PropertiesToLoad.Add("Mail"); //email-id
            searcher.PropertiesToLoad.Add("givenName");//first name
            searcher.PropertiesToLoad.Add("sn"); //Last name
            return searcher.FindOne(); //.FindAll() is also available, for example if you searching with name only //and there are chances of more than one user

in this way you can search the users from active directory, there are others properties are available to search for, you can google for it, if not found comment below, i will help.

1 comment:

prateek said...

vey informative thanks:)