安全的自托管WCF服务:从文件加载证书

时间:2019-03-05 15:35:57

标签: c# rest wcf https

我有一个自托管的WCF服务,用于发布REST Web API。该服务是通过程序配置的,目前可以通过HTTP正常运行。 现在,我需要使它通过基于证书文件的身份验证在HTTPS上运行。 我知道执行此操作的建议方法是在服务器计算机上的Windows证书存储中安装证书,但是在我的情况下这种方法是不可能的。 我需要从文件中加载证书。

经过一些重新搜索后,我编写了代码,但是当我尝试访问Web服务时,将引发System.ServiceModel.CommunicationException,并显示以下消息: 向...发出HTTP请求时发生错误。这可能是由于在HTTPS情况下未使用HTTP.SYS正确配置服务器证书。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。

这是我在服务器端的代码:

_host = new WebServiceHost(_hostedService, Uri);

//Configuring the webHttpBinding settings
var httpBinding = new WebHttpBinding();
httpBinding.Security.Mode = WebHttpSecurityMode.Transport;
httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
httpBinding.SendTimeout = new TimeSpan(0, 5, 0);
httpBinding.MaxBufferSize = 2147483647;
httpBinding.MaxBufferPoolSize = 2147483647;
httpBinding.MaxReceivedMessageSize = 2147483647;
httpBinding.ReaderQuotas.MaxDepth = 2147483647;
httpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
httpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
httpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
httpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;

//Add the endpoint with the webHttpBinding settings to the WebServiceHost configuration
_host.AddServiceEndpoint(typeof(IService), httpBinding, Uri);

ServiceDebugBehavior stp = _host.Description.Behaviors.Find<ServiceDebugBehavior>();
stp.HttpHelpPageEnabled = false;
ServiceBehaviorAttribute sba = _host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
sba.InstanceContextMode = InstanceContextMode.Single;
ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpsGetEnabled = true };
_host.Description.Behaviors.Add(smb);

X509Certificate2 trustedCertificate = new X509Certificate2("certificate.pfx", "password");
_host.Credentials.ServiceCertificate.Certificate = trustedCertificate;
_host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

这是我的客户端代码:

var httpBinding = new WebHttpBinding();
httpBinding.Security.Mode = WebHttpSecurityMode.Transport;
httpBinding.Security.Transport = new HttpTransportSecurity() { ClientCredentialType = HttpClientCredentialType.Certificate };
var httpUri = new Uri(String.Format("https://{0}:{1}", ipAddress, tcpPort));
var httpEndpoint = new EndpointAddress(httpUri);
var newFactory = new ChannelFactory<IService>(httpBinding, httpEndpoint);
newFactory.Endpoint.Behaviors.Add(new WebHttpBehavior());

X509Certificate2 trustedCertificate = new X509Certificate2("certificate.pfx", "password"); //SSL
newFactory.Credentials.ClientCertificate.Certificate = trustedCertificate;

var channel = newFactory.CreateChannel();

var response = channel.Ping("helo");

该异常抛出在最后一行(channel.Ping(“ helo”))。

我需要使其无需在服务器计算机上安装证书即可使用。

非常感谢您。

再见。

1 个答案:

答案 0 :(得分:0)

据我所知,当我们通过Https托管自托管的WCF服务时,无论使用哪种方式(加载证书文件或通过Windows证书存储区配置证书),都无法使该服务正常工作。我们唯一需要做的方法是使用以下命令手动绑定证书。

netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}

这是正式文件,希望对您有用。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate

https://docs.microsoft.com/en-us/windows/desktop/http/add-sslcert
这是我写过的一些例子。
WCF Service over HTTPS without IIS, with SSL Certificate from CERT and KEY strings or files

Chrome (v71) ERR_CONNECTION_RESET on Self Signed localhost on Windows 8 Embedded
随时告诉我是否有什么可以帮助您的。