繁体   English   中英

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

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

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

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

对于登录提供my_domain \\ jdoe的Web应用程序的用户“John Doe”

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

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

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

如果您正在考虑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;
        }
    }
}

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

我假设你在AD环境中,因为你标记了问题adsi。

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

也许我在某个地方犯了一个错误,但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

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

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

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

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)

代码从这里嘲笑它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM