我一直在使用这个函数来查找用户的用户名电子邮件地址(我的所有用户都在同一个域名上),没有问题,但是现在一些用户已经升级到Windows 10,我收到了一个错误:
服务器无法操作。
我已经参照这个问题和答案对我的代码进行了更改,所以现在我选择默认的命名上下文,但是错误仍然存在: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在我的用户使用Windows 7和8的地方,代码工作得很好。
有什么建议吗?
发布于 2016-05-19 12:19:50
我不知道它为什么会在LDAP://RootDSE上崩溃。但是,如果您只有一个域,则可以对域名进行硬编码,而不是使用RootDSE。
您可以使用域名DNS名称:
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://domain.com")或域名的可分辨名称:
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://DC=domain,DC=com")看看这有什么区别。
https://stackoverflow.com/questions/37274981
复制相似问题