We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 01/28/08


Tagged

security aspnet ActiveDirectory


Versions (?)


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


Published in: C# 


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


  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];
  6.  
  7. DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + userName +",User");
  8. retVal = (string)userEntry.Properties["fullname"].Value;
  9. return retVal;
  10. }

Report this snippet 

You need to login to post a comment.