我正在使用以下代码,用于将用户登录到针对活动目录的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
答案 0 :(得分: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...
答案 1 :(得分:0)
..并快速将组名的内容(来自Jeroenh的brillaint答案)转储到列表框中:
ListBox1.DataSource = groupnames.ToList()