[英]Retrieve user information and check if member of a group in active directory using 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
查看System.DirectoryServices.AccountManagemment命名空间。 userprincipal对象包含您需要的所有内容以及更多内容。 以下是有关如何使用此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...
..并快速将组名的内容(从Jeroenh的brillaint答案)转储到列表框中:
ListBox1.DataSource = groupnames.ToList()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.