本文介绍了在AD中查询用户并在输出对象中显示详细信息(例如标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助,我尝试通过SAMAccountName从AD查询用户,并在标签或任何VB容器/输出对象上显示重要信息,如全名,登录名,电子邮件,到期日期等。
任何想法或此示例代码
任何帮助将不胜感激。
Please help, I am trying to query a user from AD by means of SAMAccountName and display important information such as Full name, Logon Name, Email, expiration date, and the likes on a label or any VB container/output object.
Any ideas, or a sample code for this
Any help will be much appreciated.
推荐答案
Public Function GetUserInfo(ByVal UserID As String)
Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)
If UserID = "" Then
UserID = System.Security.Principal.WindowsIdentity.GetCurrent.Name.Split("\"c)(1)
End If
ADSearch.PropertiesToLoad.Add("memberOf")
ADSearch.Filter = ("(samAccountName=" & UserID & ")")
ADSearch.SearchScope = SearchScope.Subtree
Dim UserFound As SearchResult = ADSearch.FindOne()
Dim propertyCount As Integer = UserFound.Properties("memberOf").Count
If Not IsNothing(UserFound) Then
Dim DirectoryEntry As DirectoryEntry = UserFound.GetDirectoryEntry
Console.WriteLine(UserFound.GetDirectoryEntry().Properties.Item("samAccountName").Value)
Console.WriteLine(UserFound.GetDirectoryEntry().Properties.Item("userPrincipalName").Value)
Console.WriteLine(UserFound.GetDirectoryEntry().Properties.Item("sn").Value)
Console.WriteLine(UserFound.GetDirectoryEntry().Properties.Item("givenName").Value)
Console.WriteLine(UserFound.GetDirectoryEntry().Properties.Item("name").Value)
Console.WriteLine(UserFound.GetDirectoryEntry().Properties.Item("mail").Value)
Console.WriteLine(UserFound.GetDirectoryEntry().InvokeGet("PasswordExpirationDate").ToString)
Dim propertyCounter As Integer
Dim dn As String
For propertyCounter = 0 To propertyCount - 1
dn = UserFound.Properties("memberOf")(propertyCounter).ToString.Split(",")(0).Remove(0, 3)
Console.WriteLine(dn.ToString)
Next
Dim Attrib As String = "msDS-User-Account-Control-Computed"
Dim User As DirectoryEntry
User = UserFound.GetDirectoryEntry()
User.RefreshCache(New String() {Attrib})
Const UF_LOCKOUT As Integer = &H10
Dim Flags As Integer = CInt(Fix(User.Properties(Attrib).Value))
If Convert.ToBoolean(Flags And UF_LOCKOUT) Then
Console.WriteLine("Account is locked out")
Else
Console.WriteLine("Account is not locked out")
End If
End If
End Function
这篇关于在AD中查询用户并在输出对象中显示详细信息(例如标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!