Showing posts with label Current user display full name in asp.net web service without LDAP. Show all posts
Showing posts with label Current user display full name in asp.net web service without LDAP. Show all posts

Thursday, 25 June 2015

Get Current Users Display name in ASP.Net Web application

To Get current user's display name without LDAP settings.


  public string HelloUser()
        {
            string dispName = "";
            Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
           
            using (System.Web.Hosting.HostingEnvironment.Impersonate())
            {
                WindowsPrincipal principal = (WindowsPrincipal)User;
                PrincipalContext pc = new PrincipalContext(ContextType.Domain); ;
                UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
                dispName = up.DisplayName;
                
            }            
            return "Hello - " + dispName + ", your login is " + HttpContext.Current.User.Identity.Name;
        }

Note: Make sure windows impersonation is turned on in IIS.