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

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

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

\"enter

2条回答
  •  独厮守ぢ
    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.

提交回复
热议问题