How do I get the AD Display Name of the currently logged in user

前端 未结 2 1068
孤街浪徒
孤街浪徒 2020-12-05 15:49

Consider the following properties as set up in Active Directory for a user:

\"enter

相关标签:
2条回答
  • 2020-12-05 16:19

    After hours of searching for the simplest way I finally came across this

    System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
    

    I wanted to get it out there for more people like me.

    0 讨论(0)
  • 2020-12-05 16:29

    Since you're on .NET 4, you can use the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on System.DirectoryServices.AccountManagement

    Basically, you can define a domain context and easily find users and/or groups in AD:

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

    The new S.DS.AM makes it really easy to play around with users and groups in AD.

    0 讨论(0)
提交回复
热议问题