在AD中查询用户并在输出对象中显示详细信息(例如标签) [英] Query User in AD and Display details in an output object (e.g. label)

查看:221
本文介绍了在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.

推荐答案

以下代码可以帮助您入门。您可以为该函数提供登录ID,也可以获取当前已验证用户的登录ID并执行AD查找。有很多用户属性,我列出了一些。有些可以直接检索
而有些则需要多做一些工作。如果您遇到任何问题,请发布后续内容。

The below code should get you started. You can either provide a login ID to the function or it will get the login ID of the currently authenticated user and perform the AD lookup. There are many user attributes and I've listed a few. Some can be retrieved directly while others require a little more work. If there are any you are having trouble with then post a follow-up.

    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中查询用户并在输出对象中显示详细信息(例如标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆