使用用户名(vb.net)

时间:2016-06-13 07:15:49

标签: vb.net active-directory ldap

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

有人有任何想法吗?

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以尝试这种格式

''' 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