2010-09-07 99 views
1

我使用以下代碼,它可以將用戶登錄到在VB.NET中構建的應用程序與活動目錄。檢索用戶信息,並檢查是否在使用VB.NET的活動目錄中的組的成員

此代碼很好,但我需要檢索用戶的名字,姓氏,顯示名稱,並檢查用戶是否屬於某個組。

我試過很多形式的adResults.Property(「displayname」).toString()之類的東西,但不能讓它正常工作。

任何人有任何想法如何做我想要做的?

下面是我現在使用的代碼,並提前致謝。

Public Function ValidateActiveDirectoryLogin(ByVal sDomain As String, ByVal sUserName As String, ByVal sPassword As String) As Boolean 

    Dim bSuccess As Boolean = False 
    Dim adEntry As New System.DirectoryServices.DirectoryEntry("LDAP://" & sDomain, sUserName, sPassword) 
    Dim adSearcher As New System.DirectoryServices.DirectorySearcher(adEntry) 
    adSearcher.SearchScope = DirectoryServices.SearchScope.OneLevel 
    Try 
     Dim adResults As System.DirectoryServices.SearchResult = adSearcher.FindOne 
     bSuccess = Not (adResults Is Nothing) 
    Catch ex As Exception 
     bSuccess = False 
     MsgBox("Error") 
    End Try 

    Return bSuccess 

End Function 

回答

4

查看System.DirectoryServices.AccountManagemment命名空間。 userprincipal對象擁有您所需要的一切以及更多內容。 Here's an explanation關於如何使用這個API。

編輯:實際上使用真的很簡單。看看此示例代碼:

Dim userName = Environment.UserName 

' create a domain context 
Dim DC = New PrincipalContext(ContextType.Domain) 

' find a user in the domain 
Dim user = UserPrincipal.FindByIdentity(DC, userName) 

' get the user's groups 
Dim groups = user.GetGroups() 

' get the user's first and last name 
Dim firstName = user.GivenName 
Dim lastName = user.SurName 

' get the distinguishednames for all groups of the user 
Dim groupNames = From g in groups Select g.DistinguishedName 
' etc... 
+0

事實上,那絕對是超過我尋找,但如果它是獲得我所需要的唯一方法我肯定會看看它,但無論如何,從System.DirectoryServices.SearchResult對象中檢索用戶信息嗎?儘可能簡單地保持它是很好的。謝謝=) – Tom 2010-09-07 20:25:37

+0

@Tom它真的更簡單實際使用,看看我的編輯 – jeroenh 2010-09-08 07:39:17

+0

哇,這很容易。謝謝。 – Tom 2010-09-08 13:19:22

0

..和快速拋售組名的內容(從Jeroenh的brillaint回答)到一個列表框:

ListBox1.DataSource = groupnames.ToList() 
相關問題