title: SSL Cert for WCF auth
body: <p>I've developed an application which starts several WCF Service which use the SecurityMode.Message to encrypt the communication.</p>
  
  <p>It's working, but, it's very complicate, because we have to generate a SSL cert and put it in specific store, on the server and on the client.</p>
  
  <p>The problem is that the customer which will use the program:</p>
  
  <ul>
  <li>Is not in a domain(in fact, the server will certainly be in a domain, but not the client</li>
  <li>Doesn't want to buy a cert</li>
  </ul>
  
  <p>So what is my best shot? I only need to encrypt data, I don't need to ensure that I'm connecting to the right host.</p>
  
  <p>I know I'm not in the best case, but the application will be used by some specific users.</p>
  
  <p>Here is a part of my code which makes the connection:</p>
  
  <p><strong>Server side:</strong></p>
  
  <pre><code>ServiceHost host = new ServiceHost(typeof(MyServiceType))
  WSHttpBinding binding = new WSHttpBinding
  {
      ReaderQuotas = { MaxStringContentLength = int.MaxValue, MaxArrayLength = int.MaxValue,     MaxDepth = int.MaxValue, MaxBytesPerRead = int.MaxValue, MaxNameTableCharCount = int.MaxValue },
      MaxReceivedMessageSize = int.MaxValue
  };
  TimeSpan timeoutSpan = TimeSpan.FromMilliseconds(timeout);
  binding.CloseTimeout = timeoutSpan;
  binding.OpenTimeout = timeoutSpan;
  binding.ReceiveTimeout = timeoutSpan;
  binding.SendTimeout = timeoutSpan;
  binding.ReliableSession.InactivityTimeout = timeoutSpan;
  
  binding.MaxBufferPoolSize = int.MaxValue;
  binding.Security.Mode = SecurityMode.Message;
  binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
  host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings["Hostname"]);
  host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
  host
  .AddServiceEndpoint(services[port], binding, String.Format("http://localhost:{0}", port));
  </code></pre>
  
  <p><strong>Client side:</strong></p>
  
  <pre><code>string remoteAddress = String.Format("{0}://{1}:{2}", Tools.GetDescription(accessInfo.ServiceHost.Protocol), accessInfo.ServiceHost.HostName, accessInfo.PortNumber);
  
  
  // avoid seralization/deserialization problems with large XML's
  WSHttpBinding binding = new WSHttpBinding();
  binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
  binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
  binding.MaxReceivedMessageSize = int.MaxValue;
  binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
  binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
  binding.ReaderQuotas.MaxDepth = int.MaxValue;
  binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
  binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
  TimeSpan timeoutSpan = DateTime.Now.AddMinutes(30) - DateTime.Now;
  binding.CloseTimeout = timeoutSpan;
  binding.OpenTimeout = timeoutSpan;
  binding.ReceiveTimeout = timeoutSpan;
  binding.SendTimeout = timeoutSpan;
  binding.ReliableSession.InactivityTimeout = timeoutSpan;
  binding.MaxBufferPoolSize = int.MaxValue;
  
  //we set the security type
  binding.Security.Mode = SecurityMode.Message;
  binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
  binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
  
  
  ChannelFactory&lt;TService&gt; channelFactory = new ChannelFactory&lt;TService&gt;(binding, remoteAddress);
  
  _service = channelFactory.CreateChannel();
  </code></pre>
  
  <p>Please note that I removed the part concerning my custom auth to have a more clean code</p>