views:

1967

answers:

5

I've seen this question posted similarly in the past, I was hoping someone can point me in the right direction, the User.Identity.Name class returns the domain login.

Which class exposes the actual 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 returns

"John Doe"?

+1  A: 

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).

Ken Browning
+4  A: 

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.

barneytron
Use the new UserPrincipal if you have .Net 3.5 in the System.DirectoryServices.AccountManagement namespace instead. It's a much better interface to the directory objects. It exposes more properties that you used to have to get the IADsUser object for.
tvanfosson
Actually, I think it was new in .Net 3.0, not 3.5.
tvanfosson
Thanks tvanfosson! My job working with DirectoryServices ended with .NET 2.0. I'm working in a different area now and did not look at what was new in 3.5.
barneytron
+6  A: 

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;
        }
    }
}
tvanfosson
When I try this on my local machine (debug in VS) it works. When I deploy to my server it get NETWORK SERVICE. It must be a setting somewhere but I can't figure it out. Any ideas?
Loki Stormbringer
Did you try my sample using User.Identity.Name as well as Environment.UserName? For a web app, I think you'll need to use the former (I'll update my post).
tvanfosson
+1  A: 
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

Oscar Cabrero
A: 

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
HerrB