从WindowsIdentity获取用户的显示名称

Ric*_*den 11 vb.net asp.net

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

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

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

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

Joe*_*pke 14

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

Imports System.DirectoryServices.AccountManagement

...

   Dim CurrentUser As UserPrincipal = UserPrincipal.Current
   Dim DisplayName As String = CurrentUser.DisplayName 
Run Code Online (Sandbox Code Playgroud)

  • 仅当您可以从WindowsIdentity对象中获取UserPrincipal时,这才有帮助.问题依赖于处理WindowsIdentity对象 - 并从中提取用户名.我可以看到如何从WindowsIdentity对象获取WindowsPrincipal,但无法找到如何获取UserPrincipal. (2认同)

Way*_*man 2

这是有关如何执行此操作的教程:

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

  • 您链接到的那篇文章有 3 页长,我不确定要使用哪个代码。您可以在您的答案中发布具体需要的代码吗? (6认同)