Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

In my winforms application I would like to show the Display Name of the user who is currently logged on and using the application. How would I go about retrieving this information?

like image 703
Martin Avatar asked Jul 14 '11 11:07

Martin


1 Answers

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.

like image 178
marc_s Avatar answered Sep 30 '22 18:09

marc_s