我已经使用 WSHttpBindings 编写了一个自托管的 WCF 服务,并且我正在尝试使用我自己生成的证书来实现消息级安全性。不幸的是,我得到了一个隐藏的异常(通过服务跟踪查看器),指出“提供给包的凭据未被识别”。
几点注意事项:
- 这必须在代码中完成,而不是在配置中
- (服务器/客户端)证书是本地机器存储中的证书,在调试时我的用户可以访问私钥。
- 我已经用谷歌搜索了这个地狱,并找到了一个很好的资源来设置基于 WCF 消息的安全性here
我不确定我错过了什么。除了创建端点身份之外,这些东西中的大部分似乎都是直截了当的。无论我使用 DnsEndpointIdentities、基于证书的身份还是根本没有身份,它都会失败并显示相同的消息。
谁能指出我正确的方向?
服务器端:
var binding = new WSHttpBinding
{
Security =
{
Mode = SecurityMode.Message,
Message =
{
ClientCredentialType = MessageCredentialType.Certificate,
AlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256Rsa15
}
}
};
_host = new ServiceHost(this)
{
Credentials =
{
ServiceCertificate =
{
Certificate = ServiceCert
},
ClientCertificate =
{
Certificate = ClientCert,
Authentication =
{
TrustedStoreLocation = StoreLocation.LocalMachine,
RevocationMode = X509RevocationMode.NoCheck,
CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust
}
}
}
};
var address = new Uri(string.Format(@"http://serviceaddress"));
var ep = _host.AddServiceEndpoint(typeof (IService), binding, address);
ep.Address = new EndpointAddress(address, EndpointIdentity.CreateX509CertificateIdentity(ServiceCert));
_host.Open();
客户端:
var binding = new WSHttpBinding
{
Security =
{
Mode = SecurityMode.Message,
Message =
{
ClientCredentialType = MessageCredentialType.Certificate,
AlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256Rsa15
}
}
};
var address = new Uri(@"http://serviceaddress");
var endpoint = new EndpointAddress(address, EndpointIdentity.CreateX509CertificateIdentity(ServerCert));
var channelFactory = new ChannelFactory<IService>(binding, endpoint)
{
Credentials =
{
ServiceCertificate =
{
DefaultCertificate = ServerCert,
Authentication =
{
RevocationMode = X509RevocationMode.NoCheck,
TrustedStoreLocation = StoreLocation.LocalMachine,
CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust
}
},
ClientCertificate =
{
Certificate = ClientCert
}
}
};
var channel = channelFactory.CreateChannel();