ASP.Net Code to Get an Active Directory User Name from the Integrated Authentication Principal Name

/ Published in: C#
Save to your folder(s)

Typically won't work if the ASP.Net service identity (in the machine.config ProcessModel section) is a local account.

Copy this code and paste it in your HTML
  1. private string GetUserName()
  2. {
  3. string retVal = string.Empty;
  4. //Pull the username out of the domain\user string.
  5. retVal = Page.User.Identity.Name.Split('\\')[1];
  7. DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + userName +",User");
  8. retVal = (string)userEntry.Properties["fullname"].Value;
  9. return retVal;
  10. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.