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;

try

{

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 =

SearchScope.Subtree;

searcher.ReferralChasing =

ReferralChasingOption.All;

searcher.PropertiesToLoad.AddRange(properties);

searcher.Filter = filter;

SearchResult result = searcher.FindOne();

DirectoryEntry directoryEntry = result.GetDirectoryEntry();

displayName = directoryEntry.Properties[

"displayName"][0].ToString();

firstName = directoryEntry.Properties[

"givenName"][0].ToString();

lastName = directoryEntry.Properties[

"sn"][0].ToString();

email = directoryEntry.Properties[

"mail"][0].ToString();

}

catch (Exception)

{

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

}

return firstName + " " + lastName;

}

Advertisements
  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
    place.

  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

      Hi,

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

      Thanks
      Sanjiv

  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: