title: C# SOAP v1.0 project with WS-Encryption & WS-Signing
body: <p>I have been tasked with a job to create an app that connects to a web service that uses Soap V1.0 with WS-Security.</p>
  
  <p>I have been able to connect successfully using Soap UI. The Soap Service is sent via HTTPS, the SOAP message must be signed with a x509 cert and the soap body must be encrypted with their SSL.</p>
  
  <p>The Working SOAP XML looks like this:</p>
  
  <pre><code>    &lt;soapenv:Envelope xmlns:ns="http://www.qwerty.com/esi/common/1.0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
          &lt;soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"&gt;
              &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
                  &lt;wsu:Timestamp wsu:Id="TS-1D010A91DEF5E45ACE145991958150741"&gt;
                      &lt;wsu:Created&gt;2016-04-06T05:13:01.507Z&lt;/wsu:Created&gt;
                      &lt;wsu:Expires&gt;2016-04-06T05:46:21.507Z&lt;/wsu:Expires&gt;
                  &lt;/wsu:Timestamp&gt;
                  &lt;xenc:EncryptedKey Id="EK-1D010A91DEF5E45ACE145991958150539" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;
                      &lt;xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/&gt;
                      &lt;ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
                          &lt;wsse:SecurityTokenReference&gt;
                              &lt;ds:X509Data&gt;
                                  &lt;ds:X509IssuerSerial&gt;
                                      &lt;ds:X509IssuerName&gt;CN=abc CA asd,O=qwerty ,L=Sydney,C=AU&lt;/ds:X509IssuerName&gt;
                                      &lt;ds:X509SerialNumber&gt;2334442322&lt;/ds:X509SerialNumber&gt;
                                  &lt;/ds:X509IssuerSerial&gt;
                              &lt;/ds:X509Data&gt;
                          &lt;/wsse:SecurityTokenReference&gt;
                      &lt;/ds:KeyInfo&gt;
                      &lt;xenc:CipherData&gt;
                          &lt;xenc:CipherValue&gt;SAulApKrrr5M+Fhy5VWKkAug..cut for readability&lt;/xenc:CipherValue&gt;
                      &lt;/xenc:CipherData&gt;
                      &lt;xenc:ReferenceList&gt;
                          &lt;xenc:DataReference URI="#ED-1D010A91DEF5E45ACE145991958150540"/&gt;
                      &lt;/xenc:ReferenceList&gt;
                  &lt;/xenc:EncryptedKey&gt;
                  &lt;ds:Signature Id="SIG-1D010A91DEF5E45ACE145991958145938" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
                      &lt;ds:SignedInfo&gt;
                          &lt;ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"&gt;
                              &lt;ec:InclusiveNamespaces PrefixList="wsa ns soapenv" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/ds:CanonicalizationMethod&gt;
                          &lt;ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
                          &lt;ds:Reference URI="#id-1D010A91DEF5E45ACE145991958145837"&gt;
                              &lt;ds:Transforms&gt;
                                  &lt;ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"&gt;
                                      &lt;ec:InclusiveNamespaces PrefixList="ns" xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                                  &lt;/ds:Transform&gt;
                              &lt;/ds:Transforms&gt;
                              &lt;ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                              &lt;ds:DigestValue&gt;TRGWcb2SG5CRTP1FC4U1tvqg=&lt;/ds:DigestValue&gt;
                          &lt;/ds:Reference&gt;
                      &lt;/ds:SignedInfo&gt;
                      &lt;ds:SignatureValue&gt;Nnl7..cut for readability&lt;/ds:SignatureValue&gt;
                      &lt;ds:KeyInfo Id="KI-1D010A91DEF5E45ACE145991958145835"&gt;
                          &lt;wsse:SecurityTokenReference wsu:Id="STR-1D010A91DEF5E45ACE145991958145836"&gt;
                              &lt;ds:X509Data&gt;
                                  &lt;ds:X509IssuerSerial&gt;
                                      &lt;ds:X509IssuerName&gt;CN=query =qwerty ,L=Sydney,C=AU&lt;/ds:X509IssuerName&gt;
                                      &lt;ds:X509SerialNumber&gt;105asdsa571845470&lt;/ds:X509SerialNumber&gt;
                                  &lt;/ds:X509IssuerSerial&gt;
                              &lt;/ds:X509Data&gt;
                          &lt;/wsse:SecurityTokenReference&gt;
                      &lt;/ds:KeyInfo&gt;
                  &lt;/ds:Signature&gt;
                  &lt;wsse:UsernameToken wsu:Id="UsernameToken-1D010A91DEF5E45ACE145991958145533"&gt;
                      &lt;wsse:Username&gt;myusername&lt;/wsse:Username&gt;
                      &lt;wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;mypassword&lt;/wsse:Password&gt;
                      &lt;wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"&gt;nonce==&lt;/wsse:Nonce&gt;
                      &lt;wsu:Created&gt;2016-04-06T05:13:01.454Z&lt;/wsu:Created&gt;
                  &lt;/wsse:UsernameToken&gt;
              &lt;/wsse:Security&gt;
              &lt;wsa:Action&gt;http://www.qwerty.com/esi/common/1.0/getAuthenticationExpiryRequest&lt;/wsa:Action&gt;
              &lt;wsa:MessageID&gt;urn:dill:Software Version:1&lt;/wsa:MessageID&gt;
              &lt;wsa:To&gt;http://www.qwerty.com/esi/common/v1&lt;/wsa:To&gt;
          &lt;/soapenv:Header&gt;
          &lt;soapenv:Body wsu:Id="id-1D010A91DEF5E45ACE145991958145837" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
              &lt;xenc:EncryptedData Id="ED-1D010A91DEF5E45ACE145991958150540" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"&gt;
                  &lt;xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/&gt;
                  &lt;ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"&gt;
                      &lt;wsse:SecurityTokenReference wsse11:TokenType="http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#EncryptedKey" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsse11="http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"&gt;
                          &lt;wsse:Reference URI="#EK-1D010A91DEF5E45ACE145991958150539"/&gt;
                      &lt;/wsse:SecurityTokenReference&gt;
                  &lt;/ds:KeyInfo&gt;
                  &lt;xenc:CipherData&gt;
                      &lt;xenc:CipherValue&gt;HEf6zyUibGuAjgUY/cut for readability&lt;/xenc:CipherValue&gt;
                  &lt;/xenc:CipherData&gt;
              &lt;/xenc:EncryptedData&gt;
          &lt;/soapenv:Body&gt;
      &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>My Very flawed response sent via C#</p>
  
  <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
      &lt;s:Header&gt;
          &lt;a:Action s:mustUnderstand="1" u:Id="_1"&gt;http://www.qwerty.com/esi/common/1.0/getAuthenticationExpiryRequest&lt;/a:Action&gt;
          &lt;a:MessageID u:Id="_2"&gt;urn:uuid:8531ad33-56fd-4575-ba3b-cc237617f5f8&lt;/a:MessageID&gt;
          &lt;a:ReplyTo u:Id="_3"&gt;
              &lt;a:Address&gt;http://www.w3.org/2005/08/addressing/anonymous&lt;/a:Address&gt;
          &lt;/a:ReplyTo&gt;
          &lt;VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink"&gt;uIDPo5YvIPcIlqZNsE+PcW7SWU3jm5ZcyzThspFKp7rCG5JTJikACQAA&lt;/VsDebuggerCausalityData&gt;
          &lt;a:To s:mustUnderstand="1" u:Id="_4"&gt;http://it02:8080/esi2/esi-gateway/common/v1&lt;/a:To&gt;
          &lt;o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
              &lt;u:Timestamp u:Id="uuid-f39da944-9f7f-4c48-ad90-69442f958db5-1"&gt;
                  &lt;u:Created&gt;2016-04-06T05:08:02.239Z&lt;/u:Created&gt;
                  &lt;u:Expires&gt;2016-04-06T05:13:02.239Z&lt;/u:Expires&gt;
              &lt;/u:Timestamp&gt;
              &lt;o:UsernameToken u:Id="uuid-1892a0d2-1f61-4c02-b91b-b969e8ee1d44-1"&gt;
                  &lt;o:Username&gt;My Username&lt;/o:Username&gt;
                  &lt;o:Password&gt;MyPassword=&lt;/o:Password&gt;
              &lt;/o:UsernameToken&gt;
              &lt;o:BinarySecurityToken u:Id="uuid-1892a0d2-1f61-4c02-b91b-b969e8ee1d44-2" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"&gt;MIII3Q==&lt;/o:BinarySecurityToken&gt;
              &lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
                  &lt;SignedInfo&gt;
                      &lt;CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                      &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
                      &lt;Reference URI="#_1"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;qBpW9R+T2WBBySN4iBskNiWs7lk=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                      &lt;Reference URI="#_2"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;gyelv9es6SoTBFgujy2Qilngr1Y=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                      &lt;Reference URI="#_3"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;o3ibE52LCPwycD7dwAsKtJa+WMw=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                      &lt;Reference URI="#_4"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;s2MW+MnmlKI4VtLdAY6vEZbxoco=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                      &lt;Reference URI="#uuid-f39da944-9f7f-4c48-ad90-69442f958db5-1"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;7UO6LSMnFDePv9O88EiFigXEAiM=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                      &lt;Reference URI="#uuid-1892a0d2-1f61-4c02-b91b-b969e8ee1d44-1"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;ri+XlD6yZMrBP2N4mSO0t/8g9Po=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                      &lt;Reference URI="#uuid-1892a0d2-1f61-4c02-b91b-b969e8ee1d44-2"&gt;
                          &lt;Transforms&gt;
                              &lt;Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/&gt;
                          &lt;/Transforms&gt;
                          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
                          &lt;DigestValue&gt;WZH8tnKlq/XPoUn5KkoqZzdKrdE=&lt;/DigestValue&gt;
                      &lt;/Reference&gt;
                  &lt;/SignedInfo&gt;
                  &lt;SignatureValue&gt;S8U........&lt;/SignatureValue&gt;
                  &lt;KeyInfo&gt;
                      &lt;o:SecurityTokenReference&gt;
                          &lt;o:KeyIdentifier ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier"&gt;FM69HI68yR5C/F5e/WRIHII7BAM=&lt;/o:KeyIdentifier&gt;
                      &lt;/o:SecurityTokenReference&gt;
                  &lt;/KeyInfo&gt;
              &lt;/Signature&gt;
          &lt;/o:Security&gt;
      &lt;/s:Header&gt;
      &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
          &lt;AuthenticationExpiryRequest xmlns="http://www.qwerty.com/esi/common/1.0"/&gt;
      &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>My C# Code thus far:</p>
  
  <pre><code>class Program
      {
  
          private static System.ServiceModel.Channels.Binding GetCustomBinding()
          {
              System.ServiceModel.Channels.AsymmetricSecurityBindingElement asbe = new AsymmetricSecurityBindingElement();
              asbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12;
  
              asbe.InitiatorTokenParameters = new System.ServiceModel.Security.Tokens.X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.Never };
              asbe.RecipientTokenParameters = new System.ServiceModel.Security.Tokens.X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.Never };
              asbe.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.EncryptBeforeSign;
  
              asbe.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
              asbe.EnableUnsecuredResponse = true;
              asbe.IncludeTimestamp = true;
              asbe.SetKeyDerivation(false);
              asbe.DefaultAlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic128;
  
              asbe.EndpointSupportingTokenParameters.Signed.Add(new UserNameSecurityTokenParameters());
              asbe.EndpointSupportingTokenParameters.Signed.Add(new X509SecurityTokenParameters());
  
              CustomBinding myBinding = new CustomBinding();
              myBinding.Elements.Add(asbe);
              myBinding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11WSAddressing10, Encoding.UTF8));            
              HttpTransportBindingElement httpsBindingElement = new HttpTransportBindingElement();
            //  httpsBindingElement.RequireClientCertificate = true ;
  
  
              myBinding.Elements.Add(httpsBindingElement);
  
              return myBinding;
          }
  
          private static void SetClientCredentialsSecurity(ClientCredentials clientCredentials)
          {
               var store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
              store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
  
              X509Certificate2 certificate = new X509Certificate2();
              X509Certificate2 esiCertificate = new X509Certificate2();
  
              foreach (var cert in store.Certificates)
              {
                  if (cert.FriendlyName.Contains("certname"))
                  {
                      certificate = cert;
  
                  }
                  if (cert.SubjectName.Name.Contains("certname"))
                  {
                      esiCertificate = cert;
                  }
  
  
              }
  
  
  
              clientCredentials.UserName.UserName = "asd";
              clientCredentials.UserName.Password = "asd";
  
              string directoryName = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
              clientCredentials.ServiceCertificate.DefaultCertificate = esiCertificate;
              clientCredentials.ClientCertificate.Certificate = certificate;
  
          }
  
          private static ServiceReference1.Common10Client GetCredentialingClient()
          {
              ServiceReference1.Common10Client client = new ServiceReference1.Common10Client(GetCustomBinding(), new EndpointAddress(new Uri("http://asd/esi2/esi-gateway/common/v1"), new DnsEndpointIdentity("esiServer"), new AddressHeaderCollection()));
              client.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.None;
              SetClientCredentialsSecurity(client.ClientCredentials);
  
              return client;
          }
  
  
          static void Main(string[] args)
          {
  
                  using (ServiceReference1.Common10Client client = GetCredentialingClient())
                  {
                      client.Open();
  
                      ServiceReference1.getAuthenticationExpiryRequest request = new ServiceReference1.getAuthenticationExpiryRequest();
                      request.AuthenticationExpiryRequest = new ServiceReference1.AuthenticationExpiryRequest();
  
                      client.getAuthenticationExpiry(request.AuthenticationExpiryRequest);
                  }
  
  
          }
  </code></pre>
  
  <p>as you can see they are very different. What I need is:
  WS-Security - Encryption, Username, Timestamp, Signature.</p>
  
  <p>But I'm unsure how to replicate it in C#</p>