0

I has been using this function to find the users email address from their username (all my users are on the same domain) without problems, but now some of the users have been upgraded to Windows 10 and I'm getting an error:

The server is not operational

I have made changes to my code with reference to this question and the answer, so now I pick up the default naming context, but the error persists: System.DirectoryServices - The server is not operational

    Public Function UserEmail(ByRef Username As String) As String
        UserEmail = ""
        Dim deRoot As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")

        If Not deRoot Is DBNull.Value Then
            Dim defaultNamingContext As String = deRoot.Properties("defaultNamingContext").Value.ToString()
            Dim path As String
            path = "LDAP://" & defaultNamingContext


            Dim entry As New DirectoryServices.DirectoryEntry(Path)
            Dim search As New DirectoryServices.DirectorySearcher(entry)

            search.Filter = "(&(objectClass=user)(anr=" + Username + "))"
            search.PropertiesToLoad.Add("mail")

            Dim result As DirectoryServices.SearchResult
            result = search.FindOne

            If IsDBNull(result) Then
                UserEmail = ""
            Else
                UserEmail = result.Properties("mail")(0)
            End If

        End If

    Return UserEmail
End Function

The code works fine where my users are using Windows 7 and 8.

Any suggestions?

Community
  • 1
  • 1
DovesandChicks
  • 378
  • 2
  • 17

1 Answers1

0

I don't know why it would be crashing at LDAP://RootDSE. But If you only have one domain, you could hard code the domain name rather than using RootDSE.

You can use either the domain DNS name:

Dim entry As New DirectoryServices.DirectoryEntry("LDAP://domain.com")

Or the domain distinguished name:

Dim entry As New DirectoryServices.DirectoryEntry("LDAP://DC=domain,DC=com")

See if that makes any difference.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84