全名而不是User.Identity.Name中的域ID

时间:2009-01-29 23:58:37

标签: c# authentication identity adsi

User.Identity.Name属性返回域登录ID。

哪个类/属性公开实际用户名?

登录提供my_domain \ jdoe

的Web应用程序的用户“John Doe”
**User.Identity.Name -** 
Returns : *my_domain\jdoe*

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

哪个类/属性返回? ......“John Doe”

5 个答案:

答案 0 :(得分:17)

如果您正在考虑Active Directory,则需要找到与给定samAccountName对应的UserPrincipal并从中获取DisplayName属性。请注意,它可能未设置。

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

答案 1 :(得分:4)

听起来不是登录名,而是在Active Directory用户帐户的显示名称之后。您可能想要做的是进行AD搜索(DirectorySearcher)并从搜索结果属性中获取显示名称。

我假设您处于AD环境中,因为您标记了问题adsi。

注意:如果您使用的是.NET 3.5,您可能需要查看tvanfosson的帖子。

答案 2 :(得分:1)

IIdentity接口是在User.Identity上提供Name属性的接口。 IIdentity接口可以在任何数量的类上实现,这些类知道如何从数据存储(SQL Server,Active Directory等)中查找用户。

IIdentity界面没有提供“John Doe”的属性。如果该信息位于您的数据存储中,那么您将需要使用特定于该数据存储的工具来访问它。

也就是说,User.Identity返回的对象完全有可能包含“John Doe”的属性,除了IIdentity之外,你可以通过其他界面访问它(我们的自定义IIdentity实现这样做,例子)。

答案 3 :(得分:1)

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

你可以打电话

var strJonDoeName = GetFullName(User.Identity.Name)

代码从here

模拟它

答案 4 :(得分:1)

也许我在某个地方犯了一个错误,但是WinNT:// ...对我来说没有为域帐户工作。此外,如果用户与机器不在同一个域中,则PrincipalContext可能找不到所需元素(因为它在当前上下文中搜索,如果按上面使用)。

以下内容应将User.Identity.Name提供的“友好域名”转换为符合ldap的域。使用域的distinguishedName传递所需的ldap路径信息(已解决了我的跨域问题):

(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