我正在尝试将集成 Windows 身份验证与 DirectorySearcher 结合使用来识别和验证 Intranet 用户。
我设法获得了一些似乎可以解决问题的相当简单的代码,但是当我在实时服务器上尝试时,出现以下错误:
“指定的域不存在或无法联系”
我无法在实时服务器上调试应用程序,因此我将其复制到旧的开发服务器上进行测试。当我正常运行该应用程序时,它出现了相同的错误,因此我尝试在 VS 中进行调试....除了它运行良好。
我怀疑这与模拟或与 LDAP 调用有关 - 显然,当它适用于调试器时,很难确定真正的问题是什么。
但我认为你们中的一个人能够为我指明正确的方向。
我的身份验证类的片段:
Private Function GetUserID() As String
Dim sID As String = HttpContext.Current.User.Identity.Name
Return Mid(sID, InStr(sID, "\") + 1)
End Function
Private Function GetDisplayName() As String
Dim oSearcher As New DirectorySearcher
Dim oResult As SearchResult
Dim sName As String = String.Empty
With oSearcher
.Filter = String.Format("(SAMAccountName={0})", _UserID)
.PropertiesToLoad.Add("displayName")
oResult = .FindOne()
If Not oResult Is Nothing Then
sName = oResult.Properties("displayName")(0).ToString()
End If
End With
Return sName
End Function
Private Function GetEmail() As String
Dim oSearcher As New DirectorySearcher
Dim oResult As SearchResult
Dim sEmail As String = String.Empty
With oSearcher
.Filter = String.Format("(SAMAccountName={0})", _UserID)
.PropertiesToLoad.Add("mail")
oResult = .FindOne()
If Not oResult Is Nothing Then
sEmail = oResult.Properties("mail")(0).ToString()
End If
End With
Return sEmail
End Function
Private Function GetGroups() As StringCollection
Dim oSearcher As New DirectorySearcher
Dim oResult As SearchResult
Dim colGroups As New StringCollection
Dim i As Int16
With oSearcher
.Filter = String.Format("(cn=" & _UserName & ")", _UserID)
.PropertiesToLoad.Add("memberOf")
oResult = .FindOne()
If Not oResult Is Nothing Then
Dim iGroupCount As Int16 = oResult.Properties("memberOf").Count
For i = 0 To iGroupCount - 1
colGroups.Add(oResult.Properties("memberOf")(i).ToString())
Next
End If
End With
Return colGroups
End Function