2009-05-19 59 views
11

我在使用Windows身份驗證的ASP.NET應用程序中。從WindowsIdentity獲取用戶的顯示名稱

我使用HttpContext.Current.User.Identity.Name來獲取當前通過身份驗證的用戶的用戶名,該用戶名以DOMAIN \ USERNAME格式獲取用戶名。這工作正常。

有沒有一種簡單的方法可以將它轉換爲顯示名稱(例如「Richard Gadsden」),就像XP中我的開始菜單頂部顯示的那樣?

如果我必須,我想我可以通過System.DirectoryServices並查詢到ADSI,但肯定有更簡單的方法嗎?

回答

1

我猜ADSI是要走的路。這很容易。我沒有看到更簡單的方法。您只需查詢LDAP://<SID=user-sid>並獲取專有名稱屬性。

13

還有一個更簡單的方法,現在,使用System.DirectoryServices.AccountManagement

Imports System.DirectoryServices.AccountManagement 

... 

    Dim CurrentUser As UserPrincipal = UserPrincipal.Current 
    Dim DisplayName As String = CurrentUser.DisplayName 
+1

僅當您可以將UserPrincipal從WindowsIdentity對象中取出時纔有用。這個問題依賴於處理WindowsIdentity對象 - 並從中提取用戶名。我可以看到如何從WindowsIdentity對象獲取WindowsPrincipal,但無法找到如何獲取UserPrincipal。 – Veverke 2015-09-21 12:11:48