none
how to get the display name of the current user RRS feed

  • Question

  • If the account of the current user was "test.com/user", and his display name was "John Smith".

    I can get the Username using Environment.UserName, but I don't know how the get the display name....any idea?

    Wednesday, October 9, 2013 8:12 AM

Answers

  • If you are using .Net framework 4+ then you can use System.DirectoryServices.AccountManagement namespace

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find currently logged in user
    UserPrincipal user = UserPrincipal.Current;
    
    string displayName = user.DisplayName; 


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Marked as answer by 50 个字符 Wednesday, October 9, 2013 10:41 AM
    Wednesday, October 9, 2013 8:17 AM
  • Include a reference to System.DirectoryServices.AccountManagement.dll and try the following code:

          Thread.GetDomain().SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
          System.Security.Principal.WindowsPrincipal principal = (System.Security.Principal.WindowsPrincipal)Thread.CurrentPrincipal;
          string displayName;
          using (System.DirectoryServices.AccountManagement.PrincipalContext pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain)) {
            System.DirectoryServices.AccountManagement.UserPrincipal up = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
            displayName = up.DisplayName;
          }
    

    • Marked as answer by 50 个字符 Wednesday, October 9, 2013 10:41 AM
    Wednesday, October 9, 2013 10:12 AM

All replies

  • If you are using .Net framework 4+ then you can use System.DirectoryServices.AccountManagement namespace

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find currently logged in user
    UserPrincipal user = UserPrincipal.Current;
    
    string displayName = user.DisplayName; 


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Marked as answer by 50 个字符 Wednesday, October 9, 2013 10:41 AM
    Wednesday, October 9, 2013 8:17 AM
  • thanks, but I was in .Net framework 3.5 environment.
    Wednesday, October 9, 2013 9:15 AM
  • thanks, but I was in .Net framework 3.5 environment.
    Sorry about 4+, I think this works well in 3.5 also.

    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Wednesday, October 9, 2013 9:18 AM
  • Include a reference to System.DirectoryServices.AccountManagement.dll and try the following code:

          Thread.GetDomain().SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
          System.Security.Principal.WindowsPrincipal principal = (System.Security.Principal.WindowsPrincipal)Thread.CurrentPrincipal;
          string displayName;
          using (System.DirectoryServices.AccountManagement.PrincipalContext pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain)) {
            System.DirectoryServices.AccountManagement.UserPrincipal up = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
            displayName = up.DisplayName;
          }
    

    • Marked as answer by 50 个字符 Wednesday, October 9, 2013 10:41 AM
    Wednesday, October 9, 2013 10:12 AM
  • In my debug testing this also seemed much faster than using the FindByIdentity.
    Friday, January 17, 2014 6:53 PM