簡體   English   中英

安全的自托管WCF服務:從文件加載證書

[英]Secured selfhosted WCF service: load certificate from file

我有一個自托管的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”))上引發異常。

我需要使其無需在服務器計算機上安裝證書即可工作。

非常感謝你。

再見。

據我所知,當我們通過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
這是我寫過的一些例子。
不帶IIS的HTTPS上的WCF服務,帶有來自CERT和KEY字符串或文件的SSL證書

Windows 8 Embedded的自簽名本地主機上的Chrome(v71)ERR_CONNECTION_RESET
隨時告訴我有什么可以幫助的。

暫無
暫無

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

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