簡體   English   中英

如何修復“客戶端身份驗證方案‘匿名’禁止 HTTP 請求”

[英]How to fix "The HTTP request was forbidden with client authentication scheme 'Anonymous'"

我在實現與 WCF 服務對話的客戶端時遇到了一些問題。 它是由另一家公司托管的 WCF,因此我無權訪問其代碼。 我使用 Visual Studio 中的 Connected Service provider 工具來生成客戶端代碼,以便我可以發出請求,並且在我的本地機器上一切正常。 我在我們的開發環境中遇到了一個問題,如果我嘗試向客戶端發出請求,我會收到以下錯誤:

The HTTP request was forbidden with client authentication scheme 'Anonymous'

我一直在查看由 Provider 工具生成的客戶端代碼(很多代碼),我認為它可能與以下代碼塊有關。

System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
return result;

這更多地與企業網絡內的防火牆規則有關。

我使用非授權代理遇到了同樣的問題,但使用 ntlm ClientCredentialType解決了安全代理

result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;

使用 HTTPS 提供安全性。 該服務必須配置 SSL 證書。 SOAP 消息使用 HTTPS 作為一個整體受到保護。 客戶端使用服務的 SSL 證書對服務進行身份驗證。 客戶端身份驗證通過 ClientCredentialType 進行控制。

https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.basichttpsecuritymode?view=netframework-4.8

此錯誤通常表示 WCF 服務器使用證書對客戶端進行身份驗證。 當服務器和客戶端之間的信任關系尚未建立時,就會發生該錯誤。
在此處輸入圖片說明
通常,我們需要提供客戶端憑據以供服務器進行身份驗證,以便能夠調用服務。 至於需要提供什么樣的憑證,取決於服務端的綁定信息。

 BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;

即,上述錯誤表明服務器使用證書對客戶端進行身份驗證。

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

關於使用證書對客戶端進行身份驗證,您可以參考以下鏈接了解詳細信息。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/transport-security-with-certificate-authentication
如果有什么我可以幫忙的,請隨時告訴我。

感謝所有的建議。 這實際上只是由我們組織內設置的防火牆規則引起的。 刪除后,代碼按預期工作。

暫無
暫無

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

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