簡體   English   中英

“指定的域不存在或無法聯系”

[英]"The specified domain either does not exist or could not be contacted"

我正在嘗試將集成 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

我發現將 System.DirectoryServices.AccountManagement 命名空間用於此類事情要容易得多,在您的情況下 UserPrincipal 類是您的朋友。

Private Function GetEmail() As String
        Dim pc As PrincipalContext = new PrincipalContext(ContextType.Domain)
        Dim wi As WindowsIdentity = HttpContext.Current.User.Identity
        Dim up As UserPrincipal = UserPrincipal.FindByIdentity(pc, wi.Name)

        Return up.EmailAddress
End Function

我曾經遇到過同樣的問題,我發現錯誤的原因是 url 的編寫方式。

使用 AD 和 ADSI 時,請確保使用“大寫”路徑。 正如我從您的代碼中看到的,您將“cn”寫為小寫。 [GetGroups 函數]

我會嘗試的另一種方法是確保您正確使用您正在使用的“連接字符串”。

LDAP://CN=" + 用戶名 + ",OU=" + OU + ",OU=myOU,DC=myDC1,DC=myDC2";

變成

LDAP:// orgname .ad.root/CN=" + 用戶名 + ",OU=" + OU + ",OU=myOU,DC=myDC1,DC=myDC2";

其中“ orgname ”是運行 AD 的服務器名稱。

希望這可以幫助。

這是實現相同功能的另一種方法:

string fullPath = "LDAP://abc.xyz.com/DC=xyz, DC=com";
AuthenticationTypes authType = AuthenticationTypes.None;
DirectoryEntry verifiedUser = new DirectoryEntry(fullPath, txtUserName.Text.Trim(), txtPassword.Text.Trim(), authType);
verifiedUser.RefreshCache();
isAuthorisedUser = true;

這對我有用。

這很可能是權限問題:在您的開發機器上進行本地測試時,它工作正常,因為您使用您的帳戶訪問了 AD。 在 Live 服務器上,您可能在無權訪問 AD 的服務帳戶下運行應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM