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:\Windows\system32>netsh http add sslcert ipport=0.0.0.0:8080 certhash=05eef6e118e516869a75f96057a2310ecdb8a44f appid={00112233-4455-6677-8899-AABBCCDDEEFF}
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<ServiceDebugBehavior>().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<IService> cf = new ChannelFactory<IService>(binding, addr);
cf.Credentials.UserName.UserName = "TestUsername";
cf.Credentials.UserName.Password = "";
IService proxy = cf.CreateChannel();
Console.WriteLine(proxy.Do());
catch (Exception ex)
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;