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

18 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*
Run Code Online (Sandbox Code Playgroud)

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

tva*_*son 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ken*_*ing 1

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

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

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