C# – Full name rather than the domain id in User.Identity.Name


The User.Identity.Name property returns the domain login id.

Which class/property exposes the actual user name?

For user "John Doe" who logs into the web application supplying my_domain\jdoe

**User.Identity.Name -** 
Returns : *my_domain\jdoe*

Returns: *jdoe*

Which class/property returns? … "John Doe"

Best Solution

If you are thinking Active Directory, you'll need to find the UserPrincipal that corresponds to the given samAccountName and get the DisplayName property from it. Note that it may not be set.

string fullName = null;
using (PrincipalContext context = new PrincipalContext( ContextType.Domain ))
    using (UserPrincipal user
            = UserPrincipal.FindByIdentity( context,
                                            User.Identity.Name ))
        if (user != null)
            fullName = user.DisplayName;