【问题标题】:Getting user's display name from WindowsIdentity从 WindowsIdentity 获取用户的显示名称
【发布时间】:2009-05-19 15:06:20
【问题描述】:

我在一个使用 Windows 身份验证的 ASP.NET 应用程序中。

我正在使用 HttpContext.Current.User.Identity.Name 来获取当前经过身份验证的用户的用户名,这会为我获取格式为 DOMAIN\USERNAME 的用户名。这工作正常。

有没有一种简单的方法可以将其转换为显示名称(例如“Richard Gadsden”),就像在 XP 中我的开始菜单顶部显示的名称一样?

如果必须,我想我可以通过 System.DirectoryServices 并查询到 ADSI,但肯定有更简单的方法吗?

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    现在有一个更简单的方法,使用 System.DirectoryServices.AccountManagement

    Imports System.DirectoryServices.AccountManagement
    
    ...
    
       Dim CurrentUser As UserPrincipal = UserPrincipal.Current
       Dim DisplayName As String = CurrentUser.DisplayName 
    

    【讨论】:

    • 这只有在您可以从 WindowsIdentity 对象中获取 UserPrincipal 时才有帮助。这个问题依赖于使用 WindowsIdentity 对象 - 并从中提取用户名。我可以看到如何从 WindowsIdentity 对象中获取 WindowsPrincipal,但找不到如何获取 UserPrincipal。
    • 我发现here 可以简单地基于域获取 UserPrincipal。所以你的回答确实有帮助(我的反对票现在被锁定了,但是......请编辑你的答案并进行任何微小的更改,以便我可以删除我的反对票(SO的政策..)。
    【解决方案2】:

    这里有一个关于如何做到这一点的教程:

    http://www.youcanlearnseries.com/Programming%20Tips/CSharp/LDAPReader.aspx

    【讨论】:

    • 您链接到的那篇文章有 3 页长,我不确定要使用哪个代码。您可以在您的答案中发布专门需要的代码吗?
    【解决方案3】:

    我想 ADSI 是要走的路。这很容易。我没有看到更简单的方法。您只需查询LDAP://<SID=user-sid> 并获取专有名称属性。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      相关资源
      最近更新 更多