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

45,230

Solution 1

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

Solution 2

Sounds like instead of the login name, you are after the display name of an Active Directory user account. What you might want to do is to do an AD search (DirectorySearcher) and get the display name from the search result property.

I'm assuming that you are in an AD environment, since you tagged the question adsi.

Note: If you are working with .NET 3.5, you might want to look at tvanfosson's post.

Solution 3

The IIdentity interface is that which provides the Name property on User.Identity. The IIdentity interface can be implemented on any number of classes which know how to lookup users from a data-store (SQL Server, Active Directory, etc).

There is no property of the IIdentity interface which provides "John Doe". If that information is located in your data-store then you'll need to use the tools specific to that data-store to access it.

That said, its entirely possible that the object which is returned by User.Identity has a property which contains "John Doe" that you might be able to access through some other interface besides IIdentity (our custom IIdentity implementation does this, for example).

Solution 4

using System.DirectoryServices;


public static string GetFullName(string strLogin)
    {
        string str = "";
        string strDomain;
        string strName;

        // Parse the string to check if domain name is present.
        int idx = strLogin.IndexOf('\\');
        if (idx == -1)
        {
            idx = strLogin.IndexOf('@');
        }

        if (idx != -1)
        {
            strDomain = strLogin.Substring(0, idx);
            strName = strLogin.Substring(idx + 1);
        }
        else
        {
            strDomain = Environment.MachineName;
            strName = strLogin;
        }

        DirectoryEntry obDirEntry = null;
        try
        {
            obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
            System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
            object obVal = coll["FullName"].Value;
            str = obVal.ToString();
        }
        catch (Exception ex)
        {
            str = ex.Message;
        }
        return str;
    }

and the you can just call

var strJonDoeName = GetFullName(User.Identity.Name)

code mock it from here

Solution 5

Maybe I have made a mistake somewhere, but WinNT://... hasn't worked for domain accounts for me. Additionally, if the user isn't in the same domain as the machine, than PrincipalContext may not find the wanted element (as it searches in the current context, if used as above).

The following should translate the "friendly domain name" as provided by User.Identity.Name to an ldap compliant domain. Using the distinguishName of the domain delivers the needed ldap path information (which has solved my cross domain problem):

(VB.NET, sorry)
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices.ActiveDirectory
...

Dim strUserName As String
Dim objDirContext As DirectoryContext
Dim objContext As PrincipalContext
Dim objPrincipal As Principal
Dim strLDAPDomainPath As String
...

// User.Identity.Name delivers domain\account or account@domain
// Split User.Identity.Name in domain and account as specified above
strDomain = "my_domain"
strAccount = "jdoe"

// Get LDAP domain relative path (distinguishName) from short domain name (e.g. my_domain -> us.my_domain.com -> DC=us,DC=my_domain,DC=com)
Try
    objDirContext = New DirectoryContext(DirectoryContextType.Domain, strDomain)
    strLDAPDomainPath = Domain.GetDomain(objDirContext).GetDirectoryEntry.Properties("distinguishedName").Value.ToString
Catch objException As DirectoryServicesCOMException
    Throw New Exception("Couldn't get LDAP domain: " & objException.Message)
End Try

// Find user in LDAP
// Nothing results in using current domain controller
Try
   objContext = New PrincipalContext(ContextType.Domain, Nothing, strLDAPDomainPath)
   objPrincipal = Principal.FindByIdentity(objContext, IdentityType.Name, strAccount)

   If Not IsNothing(objPrincipal) Then
      strUserName = objPrincipal.DisplayName
   End If
Catch objException As Exception
   Throw New Exception("Couldn't get user display name: " & objException.Message)
End Try

// strUserName should now contain the wanted full name
Share:
45,230
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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"