我一直在使用此功能从用户名中找到用户的电子邮件地址(我的所有用户都在同一个域中)没有问题,但现在有些用户已经升级到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的情况下,代码可以正常工作。
有什么建议吗?
答案 0 :(得分:0)
我不知道为什么它会在LDAP:// RootDSE崩溃。但是如果您只有一个域,则可以对域名进行硬编码,而不是使用RootDSE。
您可以使用域名DNS:
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://domain.com")
或域名专有名称:
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://DC=domain,DC=com")
看看是否有所不同。