【问题标题】:How do I implement WS-security in WCF client service (timestamp, usernametoken, signature)如何在 WCF 客户端服务中实现 WS 安全性(时间戳、用户名令牌、签名)
【发布时间】:2020-12-20 18:22:39
【问题描述】:

我需要使用 WS-Security 实现 WCF 请求。标头必须有如下标签(Signature、UsernameToken 和 Timestamp):

<soapenv:Header>
   <wsse:Security>
     <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">...
     <wsse:UsernameToken wsu:Id="UsernameToken-DCF9C511">...
     <wsu:Timestamp wsu:Id="TS-DCF9C5119CC59E9AE2159888852210410">...
   </wsse:Security>
</soapenv:Header>

我已尝试使用此代码,我在标题中得到“签名”和“时间戳”标签,但“用户名令牌”标签不存在:

System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

Servicio.RecaudoWSPortClient client = new Servicio.RecaudoWSPortClient();
                    
//Configuration certificate
X509Certificate2 cert = new X509Certificate2();
cert.Import(@"C:\Users\jdduitama\Desktop\SCRIPTS\bis\Certificado\PKCS C#\PRUEBA.pfx", "PRUEBA", X509KeyStorageFlags.DefaultKeySet);

X509Certificate2 cert2 = new X509Certificate2();
cert2.Import(@"C:\Users\jdduitama\Desktop\SCRIPTS\bis\Certificado\Certificado.cer", "", X509KeyStorageFlags.DefaultKeySet);

//Configuration Custom Binding
TextMessageEncodingBindingElement textEncoding = new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.Soap11 };
HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement { RequireClientCertificate = true };
TransportSecurityBindingElement sec = SecurityBindingElement.CreateCertificateOverTransportBindingElement();                    
sec.EnableUnsecuredResponse = true;
                    
CustomBinding customBinding = new CustomBinding(sec, textEncoding, httpsTransport);
                                        
client.Endpoint.Binding = myBinding;
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.PeerOrChainTrust;
client.ClientCredentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.Offline;
client.ClientCredentials.ServiceCertificate.DefaultCertificate = cert2;
client.ClientCredentials.ClientCertificate.Certificate = cert;

client.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://myservice.com/service");
client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 30);

client.ClientCredentials.UserName.UserName = "USERNAME";
client.ClientCredentials.UserName.Password = "PASSWORD";
                   
responseConsulta = client.ConsultaPorValidacion(requestConsulta);

我认为解决方案应该在绑定安全配置中,因为如果我在配置中使用安全模式“TransportWithMessageCredential”,我会在 Header 中获得 usernameToken 但我会丢失“Signature”和“TimeStamp”

<binding name="RecaudoWSPortSoap11">
         <security mode="TransportWithMessageCredential" />
</binding>

【问题讨论】:

  • 问题解决了吗?如果我的回复对您有帮助,您可以将其标记为答案。如果问题没有解决,请告诉我。
  • 实际上不,我尝试了每种身份验证模式,但没有任何帮助我,我使用这种模式:AsymmetricSecurityBindingElement and MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12;在这种模式下,我得到了三个标签和其他更多标签,但是这种模式会加密正文和标题,因此服务无法工作

标签: c# wcf soap web-config app-config


【解决方案1】:

如果安全模式设置为TransportWithMessageCredential,会覆盖自定义绑定中的安全模式,所以我认为这不是解决办法。

WCF为自定义绑定提供了18种认证方式,或许你可以试试UserNameOverTransport:

TransportSecurityBindingElement sec = SecurityBindingElement.CreateUserNameOverTransportBindingElement();

您也可以尝试其他身份验证方案。更多其他认证方案,可以参考这个链接:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/securitybindingelement-authentication-modes

【讨论】:

  • 使用这种模式“UserNameOverTransport”我得到了这些标签(时间戳和用户名令牌)但签名丢失了
  • 也许你可以试试Messagecontract。 Messagecontract 允许您添加自定义消息头。关于Messagecontract的更多信息,可以参考这个链接:docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/…
猜你喜欢
  • 2014-06-30
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 2017-01-30
  • 1970-01-01
  • 2011-10-25
  • 2012-03-11
  • 1970-01-01
相关资源
最近更新 更多