Home > Computers and Internet > Get User information from Active Directory Against Username

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;


  1. 10/08/2014 at 5:13 am

    This is my first time go to see at here and i am actually happy to read all at single

  2. 21/09/2014 at 12:17 am

    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.

    • 22/09/2014 at 9:12 am


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


  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: