/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public UserPrincipal GetLoggedInUser() { try { //Get the principal logged in user WindowsPrincipal principal = System.Threading.Thread.CurrentPrincipal as WindowsPrincipal; //split the principal identity name from the domain and put them in an array string delimiter = "\\"; string userAndDomain = principal.Identity.Name.ToString(); string[] arrayName = userAndDomain.Split(delimiter.ToCharArray()); //Get the context of this action based on the first item in the arrayName which is the domain //Get the userprincipal (user) by matching the username in active directory and the name in the array UserPrincipal p = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, arrayName[1]); //make sure the userprincipal is a directoryentry then get the user { DirectoryEntry user = (DirectoryEntry)p.GetUnderlyingObject(); //The EmpID is located in the wwwHomePage field in AD if (user.Properties["wwwHomePage"].Count > 0) { //set a variable to the wwwHomePage property so we can return it empID = Convert.ToInt32(user.Properties["wwwHomePage"].Value); } else { empID = 0; } } return p; } catch (Exception ex) { return null; } }