Get User information from Active Directory Against Username

private string GetUserName()


string displayName = string.Empty;

string firstName = string.Empty;

string lastName = string.Empty;

string email = string.Empty;



string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", Environment.UserName);

string domain = Environment.UserDomainName;

string[] properties = new string[] { "fullname" };

DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);

DirectorySearcher searcher = new DirectorySearcher(adRoot);

searcher.SearchScope =


searcher.ReferralChasing =



searcher.Filter = filter;

SearchResult result = searcher.FindOne();

DirectoryEntry directoryEntry = result.GetDirectoryEntry();

displayName = directoryEntry.Properties[


firstName = directoryEntry.Properties[


lastName = directoryEntry.Properties[


email = directoryEntry.Properties[



catch (Exception)


if (firstName.Equals(string.Empty)) firstName = Environment.UserName;


return firstName + " " + lastName;



3 thoughts on “Get User information from Active Directory Against Username

  1. Hmm it seems like your blog ate my first comment
    (it was extremely long) so I guess I’ll just sum it up what I
    had written and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new
    to everything. Do you have any recommendations for novice blog writers?
    I’d genuinely appreciate it.

    • Hi,

      I would say, just start sharing your precious knowledge with other, choose any platform people will find you.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s