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

adsi_help picture adsi_help · Jan 30, 2009 · Viewed 44.5k times · Source

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*

**System.Environment.UserName**
Returns: *jdoe*

Which class/property returns? ... "John Doe"

Answer

tvanfosson picture tvanfosson · Jan 30, 2009

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;
        }
    }
}