| You Should Drive a Lamborghini |
A true daredevil, you’re always in search of a new rush. Clearly, you’re a total speed demon… just don’t get caught!
|


Life Universe and Everything Distributed
| You Should Drive a Lamborghini |
A true daredevil, you’re always in search of a new rush. Clearly, you’re a total speed demon… just don’t get caught!
|


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-AABBCC
DDEEFF}
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.
[code:c#]
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;
}
}
}
[/code]
Update:
If you see a NetSh exception as shown below.
netsh>http add sslcert ipport=192.168.1.64:8081 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?
