我正在尝试创建一个安全的网络服务。
这是合同和服务实施
[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 请求。”
造成这种情况的另一个原因是您所访问的服务器上的证书本身。确保您已导入私钥。在 MMC 中,这将显示为“友好名称”。这花了我几天的时间才弄清楚。一旦我导入私钥,匿名错误就消失了,一切都很好!
我们收到此错误消息,对于我们来说,解决方案是尚未为脚本启用处理程序映射功能权限。您可以在 IIS 中的“处理程序映射”>“编辑功能权限”下启用此功能,或者通过将
Script
添加到 web.config 中 accessPolicy
节点的 handlers
属性来启用此功能:
<system.webServer>
<handlers accessPolicy="Script">
...
</handlers>
</system.webServer>
如果您运行自托管 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>
参考资料:
我遇到过这样的错误。该证书是子域通配符证书。我必须将私钥导入 LocalMachine 的“受信任的人”存储中,此错误消失了。正如其他人指出的那样,您还可以尝试将私钥导入 LocalMachine 的“受信任的根”存储中。
我之前遇到过这个问题,遵循以下步骤有助于解决该问题。
在提升的环境中打开 cmd 并运行以下命令 netsh winhttp 设置代理 127.0.0.1:8888
看看这是否解决了问题,并打开了fiddler
稍后使用以下命令将其重置回来 netsh winhttp 重置代理
有一个注册表项位于
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL
。将该值设置为 0
可以解决该问题。 此处概述了其他一些潜在的解决方案。我有一个现有的答案,它引用了方法 3,并从引用的页面获得了许多赞成票,但由于未知原因被版主删除了。重新发布,以防将来对其他人有帮助。