title: WSDL binding with both certificate and username/password
body: <p>I've to make a C# dll to use webservices in some applications (as a com object). The server requires an authentification using both certificate and username/password.</p>
  
  <p>I've tried many solutions but none worked so i'm looking for a solution.</p>
  
  <p>My last try was a custom bindng like this :</p>
  
  <pre><code>// Custom binding
  CustomBinding binding = new CustomBinding();
  var userNameToken = new UserNameSecurityTokenParameters();
  userNameToken.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
  
  var securityElement = new AsymmetricSecurityBindingElement();
  securityElement.IncludeTimestamp = true;
  securityElement.RecipientTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.SubjectKeyIdentifier, SecurityTokenInclusionMode.Never);
  securityElement.InitiatorTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.SubjectKeyIdentifier, SecurityTokenInclusionMode.AlwaysToRecipient);
  securityElement.DefaultAlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256;
  securityElement.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
  securityElement.SetKeyDerivation(false);
  securityElement.EndpointSupportingTokenParameters.SignedEncrypted.Add(userNameToken);
  securityElement.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.EncryptBeforeSign;
  securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11;
  binding.Elements.Add(securityElement);
  
  var encodingElement = new TextMessageEncodingBindingElement();
  encodingElement.MessageVersion = MessageVersion.Soap12WSAddressingAugust2004;
  binding.Elements.Add(encodingElement);
  
  var httpElement = new HttpsTransportBindingElement();
  httpElement.UseDefaultWebProxy = true;
  binding.Elements.Add(httpElement); 
  
  // Create the endpoint address. Note that the machine name 
  EndpointAddress ea = new EndpointAddress("https://myURL/userservice");
  
  // Create the client. 
  UserServiceClient sNext = new UserServiceClient(binding, ea);
  
  // Utilisation du WebService
  sNext.ClientCredentials.UserName.UserName = "user";
  sNext.ClientCredentials.UserName.Password = "pwd";
  
  sNext.ClientCredentials.ClientCertificate.Certificate = autCertificat;
  sNext.ClientCredentials.ServiceCertificate.DefaultCertificate = autCertificat;
  sNext.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
  
  sNext.MyService();
  </code></pre>
  
  <p>Thanks for your help.
  Matt</p>
  
  <p>Edit : </p>
  
  <p>My C# project contains a Service Reference generating from a WSDL file on a server.
  I compile it to make a dll that can be used in Visual FoxPro clients that used the WebServices of the WSDL.</p>
  
  <p>If i go to the URL of the WebServices in a browser, it ask my first a certificate (that i choose from the list) and second i've to enter a user/password : in the browser it works fine.</p>
  
  <p>Now i've to call this Webservices from my DLL but i don't know how to define the binding and the endpoint to have the same authentification process.</p>
  
  <p>Thx</p>