【问题标题】:Get first and last name from active directory using username (vb.net)使用用户名(vb.net)从活动目录中获取名字和姓氏
【发布时间】:2016-06-13 07:15:48
【问题描述】:

我想只使用用户名从活动目录中获取名字和姓氏。下面的代码有效,但没有为没有姓氏的用户显示任何内容。我需要这个来只显示没有姓氏的人的名字。

有人知道吗?

Private Function GetActiveDirUserDetails(ByVal userid As String) As String
    Dim dirEntry As System.DirectoryServices.DirectoryEntry
    Dim dirSearcher As System.DirectoryServices.DirectorySearcher
    Dim domainName As String = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName
    Try
        dirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" & domainName)
        dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
        dirSearcher.Filter = "(samAccountName=" & userid & ")"

        dirSearcher.PropertiesToLoad.Add("GivenName")
        'Users e-mail address
        dirSearcher.PropertiesToLoad.Add("sn")
        'Users last name
        Dim sr As SearchResult = dirSearcher.FindOne()
        If sr Is Nothing Then 'return false if user isn't found 
            Return False
        End If
        Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry()
        Dim userFirstLastName = de.Properties("sn").Value.ToString() + ", " + de.Properties("GivenName").Value.ToString()
        Return userFirstLastName
    Catch ex As Exception ' return false if exception occurs 
        Return ex.Message
    End Try
End Function

编辑

我找到了解决方案,很简单。

Private Function GetActiveDirUserDetails(ByVal username As String) As String
        Dim dirEntry As System.DirectoryServices.DirectoryEntry
        Dim dirSearcher As System.DirectoryServices.DirectorySearcher
        Try
            dirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://172.17.25.10:389/DC=bsidomain,DC=com")
            dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
            dirSearcher.Filter = "(samAccountName=" & username & ")"
            dirSearcher.PropertiesToLoad.Add("GivenName")
            dirSearcher.PropertiesToLoad.Add("sn")
            Dim sr As DirectoryServices.SearchResult = dirSearcher.FindOne()
            If sr Is Nothing Then
                Return False
            End If

            Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry()

            Dim ObjFirstName As String = ""
            Dim ObjLastName As String = String.Empty

            Try
                ObjFirstName = de.Properties("GivenName").Value.ToString()
                ObjLastName = de.Properties("sn").Value.ToString()

            Catch ex As Exception
                ObjFirstName = de.Properties("DisplayName").Value.ToString()
            End Try

        MsgBox(ObjFirstName + ObjLastName)

        Catch ex As Exception ' return false if exception occurs 
            Return ex.Message
        End Try
    End Function

【问题讨论】:

    标签: vb.net active-directory ldap


    【解决方案1】:

    我在这里尝试过,它成功了:

    Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry()
    Dim userFirstName = de.Properties("GivenName").Value.ToString()
    Dim userLastName = de.Properties("sn").Value.ToString()
    Return userFirstName + ", " + userLastName
    

    【讨论】:

    • 谢谢!但它仍然不适合我,我不知道为什么
    【解决方案2】:

    你可以试试这个格式

    ''' other code removed for brevity
    Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry()
    Dim names = New List(Of String)
    If de.Properties("sn") <> Nothing AndAlso _
        de.Properties("sn").Value <> Nothing AndAlso _
        String.IsNullOrEmpty(de.Properties("sn").Value.ToString()) = False Then
        names.Add(de.Properties("sn").Value.ToString())
    End If
    If de.Properties("GivenName") <> Nothing AndAlso _  
        de.Properties("GivenName").Value <> Nothing AndAlso _
        String.IsNullOrEmpty(de.Properties("sn").Value.ToString()) = False Then
        names.Add(de.Properties("GivenName").Value.ToString())
    End If
    Dim userFirstLastName = String.Join(", ", names)
    Return userFirstLastName
    

    【讨论】:

    • 我在使用这个时遇到了错误:在函数中它说表达式是预期的
    • @C.PurS 次要语法错误。我修好了。
    • @C.PurS 您当前代码不起作用的原因是因为在构造字符串时,如果任何部分为 Nothing,那么整个表达式将变为 Nothing。
    • 函数仍然报错?你导入了吗System.Linq
    • 您是指 System.Data.Linq 吗?如果是,那么我已经导入了它。但是当我尝试导入 System.Linq 时,它给了我这个警告:system,linq 不包含任何公共成员。而且功能仍然给我错误
    猜你喜欢
    • 2016-08-01
    • 2019-01-06
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2015-04-30
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多