Thoughts on BasicHttpBinding, Security and SSL

  1. BasicHttpBinding,  can there be security?. Yes! The point is this binding can be secured and that too with transport level security and restricted to https(SSL) or message level Check this post for details.
  2. Another common question is about user name security tokens. Please note that user name security tokens cannot be send on the clear. There are quite a few posts on the forums with this quesiton. The point is you need to have a security binding element within the binding you use to send this token across cause WCF doesnt allow clear text tokens to be transfered over the wire. Now this being said, the security may be at the transport or the message layer and not restricted to just message. This means you can deploy something with SSL and use text encoding and send a UserNamePassword token cause the security here is done by the trasnport. The only requirement is that there be a security binding element somewhere in the channel stack so that there is a gurantee that the user name and password is securely tranfered across the wire.
  3. Do we need valid certs to use SSL? No we dont need a valid cert. The Sample below shows how you can use a self made cert to setup SSL on a port using netsh on vista and send a username token. Please note this is not production code and only shows the capabilites. Ideally you require IIS to be setup with a valid cert and make sure your requested name matches the cert.

This sample basically shows the use of the PermissiveCertificatePolicy that enables use of self made certs.

You need to setup SSL for your security element. Username tokens cannot be send clear ont he wire.

If you are on vista setup the certificate for SSL using netsh with something like this.

C:Windowssystem32>netsh http add sslcert ipport= certhash=05eef6e118e516869a75f96057a2310ecdb8a44f appid={00112233-4455-6677-8899-AABBCC

SSL Certificate successfully added

The code blow shows a self hosted service with a permissive certificate policy so that you can use certs made using makecert etc.



using System;using System.Collections.Generic;using System.Text;using System.ServiceModel;using System.ServiceModel.Description;using System.Security.Cryptography.X509Certificates;using System.Net;using System.IdentityModel.Selectors;namespace SimpleUNP{    [ServiceContract]    interface IService    {        [OperationContract]        string Do();    }    public class ServiceImplementation : IService    {        public string Do()        {            return "Hello Service";        }    }    public class CustomUNPValidator : UserNamePasswordValidator    {        public override void Validate(string userName, string password)        {            Console.BackgroundColor = ConsoleColor.Red;            Console.WriteLine("Username at the service : " + userName);            Console.ResetColor();        }    }    class Program    {        static void Main(string[] args)        {            string addr = "https://localhost:8080/MyService";            Uri[] baseAddrs = new Uri[] { new Uri(addr) };            BasicHttpBinding binding = new BasicHttpBinding();            binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;            binding.Security.Message.ClientCredentialType =                         BasicHttpMessageCredentialType.UserName;            using (ServiceHost sh = new ServiceHost(typeof(ServiceImplementation), baseAddrs))            {                sh.Description.Behaviors.Find().IncludeExceptionDetailInFaults = true;                sh.Credentials.UserNameAuthentication.UserNamePasswordValidationMode =                          System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;                sh.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator =                           new CustomUNPValidator();                sh.AddServiceEndpoint(typeof(IService), binding, "");                sh.Open();                Console.WriteLine("Host listening on " + sh.BaseAddresses[0].AbsolutePath);                try                {                    // WARNING: This code is only needed for test                    // certificates such as those created by makecert. It is                    // not recommended for production code.                    PermissiveCertificatePolicy.Enact("CN=localhost");                    ChannelFactory cf = new ChannelFactory(binding, addr);                    cf.Credentials.UserName.UserName = "TestUsername";                    cf.Credentials.UserName.Password = "";                    IService proxy = cf.CreateChannel();                    Console.WriteLine(proxy.Do());                }                catch (Exception ex)                {                }            }        }    }    // WARNING: This code is only needed for test certificates    // such as those created by makecert. It is    // not recommended for production code.    class PermissiveCertificatePolicy    {        string subjectName;        static PermissiveCertificatePolicy currentPolicy;        PermissiveCertificatePolicy(string subjectName)        {            this.subjectName = subjectName;            ServicePointManager.ServerCertificateValidationCallback +=                new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);        }        public static void Enact(string subjectName)        {            currentPolicy = new PermissiveCertificatePolicy(subjectName);        }        bool RemoteCertValidate(object sender,                                X509Certificate cert,                                X509Chain chain,                                System.Net.Security.SslPolicyErrors error)        {            if (cert.Subject == subjectName)            {                return true;            }            return false;        }    }}



If you see a NetSh exception as shown below.

netsh>http add sslcert ipport= certhash=1ad46ef8d371e0746e534e840d7ec45105777867 appid={e0cf4009-7f7e-4e59-b986-96f0ad2063af}
SSL Certificate add failed, Error: 1312
A specified logon session does not exist. It may already have been terminated.

You migth encounter this if your certificate is stored in the CurrentUser personal store. Try moving it from there to the LocalMachine store.


It’s time to elevate your sense of self, dear Virgo. You are just as good as anyone else, so why don’t you believe it? The problem is that you are very sensitive about having an ego. Even though you are aware that we all have egos, you try to punish yourself for its existence! This is a noble endeavor, but this kind of zealous behavior doesn’t do you or anyone else any good. You’ll never be perfect, and neither will anyone else. So what are you worrying about?