[英]Getting 403 Forbidden error. The HTTP request was forbidden with client authentication scheme 'Anonymous'
[英]The HTTP request was forbidden with client authentication scheme 'Anonymous'. The remote server returned an error: (403) Forbidden
我正在嘗試創建一個安全的網絡服務。
這是合同和服務的實現
[ServiceContract()]
public interface ICalculatorService
{
[OperationContract()]
int Add(int x, int y);
}
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class CalculatorService : ICalculatorService
{
public int Add(int x, int y)
{
return x + y;
}
}
這里我有服務代碼
var b = new WSHttpBinding(SecurityMode.Transport);
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
b.Security.Message.ClientCredentialType = MessageCredentialType.None;
Type contractType = typeof(ICalculatorService);
Type implementedContract = typeof(CalculatorService);
Uri baseAddress = new Uri("https://localhost:8006/CalculatorService");
ServiceHost sh = new ServiceHost(implementedContract);
sh.AddServiceEndpoint(contractType, b, baseAddress);
//ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
//sm.HttpsGetEnabled = true;
//sm.HttpsGetUrl = new Uri("https://localhost:8006/CalculatorServiceMex");
//sh.Description.Behaviors.Add(sm);
sh.Credentials.Peer.PeerAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerTrust;
sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost");
sh.Open();
Console.WriteLine("Service is Listening");
Console.ReadLine();
sh.Close();
這是客戶端代碼
var b = new WSHttpBinding(SecurityMode.Transport);
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
b.Security.Message.ClientCredentialType = MessageCredentialType.None;
var factory = new ChannelFactory<ICalculatorService>(b);
factory.Credentials.Peer.PeerAuthentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerTrust;
factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.TrustedPeople, X509FindType.FindBySubjectName, "localhost");
var client = factory.CreateChannel(new EndpointAddress(new Uri("https://localhost:8006/CalculatorService")));
ServicePointManager.ServerCertificateValidationCallback =
((sender, certificate, chain, sslPolicyErrors) =>
{
return true;
});
ICommunicationObject comObject = client as ICommunicationObject;
int result = -1;
try
{
comObject.Open();
result = client.Add(10, 2);
}
catch (Exception ex)
{
}
Console.WriteLine(string.Format("Service say 10 + 2 = {0}", -1));
Console.ReadLine();
該服務運行良好,並且在進行 ServicePointManager.ServerCertificateValidationCallback 檢查時沒有策略錯誤,並構建了正確的證書鏈。
我的 CA 位於受信任的根目錄中,服務器/客戶端證書位於 TrustedPeople 存儲中。 此外,如果我從瀏覽器導航到該站點,我會看到返回的頁面。 沒有錯誤
我已將 IIS 更新為我認為需要的,將證書綁定在 IIS 中
並通過下面的命令行。
我已將 SSL 設置設置為接受證書
並啟用匿名身份驗證。
有誰知道我沒有正確完成哪些步驟或發現任何問題? 我不斷收到相同的錯誤“客戶端身份驗證方案‘匿名’禁止 HTTP 請求。”
另一個原因是您正在訪問的服務器上的證書本身。 確保您已導入 PRIVATE KEY。 在 MMC 中,這將顯示為“友好名稱”。 這花了我幾天的時間才弄清楚。 一旦我導入了私鑰,匿名錯誤就消失了,一切都很好!
當您在 IIS 中使用安全類型傳輸和客戶端憑據類型證書托管 WCF 服務時,請將您的客戶端證書放在根存儲並在 IIS 中啟用匿名身份驗證。 在 IIS 中啟用匿名身份驗證。 但最重要的是,將您的證書添加到根存儲。
如果您運行自托管 WCF 服務(沒有 IIS),您可以通過將以下設置添加到配置文件(在服務器中)來啟用匿名客戶端:
<behaviors>
<serviceBehaviors>
<behavior name="limitedAuthBehavior">
<serviceAuthenticationManager authenticationSchemes="Anonymous, Basic, Digest, Negotiate"/>
<!-- ... -->
</behavior>
</serviceBehaviors>
</behaviors>
此外,將 clientCredentialType 設置為“InheritedFromHost”:
<bindings>
<basicHttpBinding>
<binding name="secureBinding">
<security mode="Transport">
<transport clientCredentialType="InheritedFromHost" />
</security>
</binding>
</basicHttpBinding>
</bindings>
參考:
我們收到此錯誤消息,對我們而言,解決方案是尚未為腳本啟用處理程序映射功能權限。 您可以在 IIS 中的 Handler Mappings > Edit Feature Permissions 下啟用此功能,或者通過將Script
添加到 web.config 中handlers
節點的accessPolicy
屬性:
<system.webServer>
<handlers accessPolicy="Script">
...
</handlers>
</system.webServer>
我有這種錯誤。 該證書是一個子域通配符。 我不得不將私鑰導入 LocalMachine 的“受信任的人”商店,這個錯誤消失了。 就像其他人指出的那樣,您也可以嘗試將私鑰導入 LocalMachine 的“受信任的根”存儲。
我之前遇到過這個問題,按照以下步驟幫助解決了這個問題。
在提升中打開一個 cmd 並在命令netsh winhttp set proxy 127.0.0.1:8888下運行
看看這是否解決了問題,並打開了提琴手
稍后使用以下命令重置它netsh winhttp 重置代理
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.