How I can get my first name last name with c# in my system (logging in windows with Active Directory username and pass)?
Is it possible to do that without going to the AD?
How I can get my first name last name with c# in my system (logging in windows with Active Directory username and pass)?
Is it possible to do that without going to the AD?
This solution didn't work for me but this function worked great:
You should call it that way:
(Found it here).
There is an easier way to do this:
The problem with the approved answer is that if you have a policy of Lastname, Firstname in place, then DisplayName gives Smith, John, not John Smith. There are two ways to get the correct form, the userPrincipal.Name property contains "John Smith (jsmith1)" so you could use this, and just string.Split on "(". Or use the following:
This would give the required form "John Smith" as required by the original poster.
If you're using .Net 3.0 or higher, there's a lovely library that makes this practically write itself.
System.DirectoryServices.AccountManagement
has aUserPrincipal
object that gets exactly what you are looking for and you don't have to mess with LDAP or drop to system calls to do it. Here's all it'd take:Note: you don't actually need the principal if you already have the username, but if you're running under the users context, it's just as easy to pull it from there.