Click here to Skip to main content
11,642,381 members (68,056 online)
Click here to Skip to main content
Add your own
alternative version

An easy way to use certificates for WCF security

, 30 Apr 2007 MIT 283.9K 5.4K 128
An easy solution to use certificates on Windows Communication Foundation loading the certificates from files
wcfcertificates.zip
WCFCertificates
Bin
DevAge.ServiceModel.dll
Client
Client.cer
Client.pfx
Client.pvk
Properties
Service References
MathService.map
DevAge.ServiceModel
Configuration
Properties
Documents
Server
App_Code
App_Data
Client.cer
Server.cer
Server.pfx
Server.pvk
Bin
DevAge.ServiceModel.dll
MathService.svc
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Security.Cryptography.X509Certificates;

namespace DevAge.ServiceModel
{
    /// <summary>
    /// A class that derive from the ServiceHost system class to automatically set the 
    /// server certificate used for service authentication.
    /// This class set the Credentials.ServiceCertificate.Certificate property override any certificate configuration.
    /// Consider anyway that you must correctly configure the binding security.
    /// </summary>
    public class CertificateServiceHost : ServiceHost
    {
        public CertificateServiceHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();

            //Check if there is a valid configuration section
            Configuration.Section section = Configuration.Section.GetSection();
            if (section == null || section.Services == null)
                return;

            //Check if there is a valid configuration for this service
            Configuration.ServiceElement element = section.Services.GetElementByKey(Description.Name);
            if (element == null)
                return;

            X509Certificate2 serverCertificate = element.GetServerCertificate();

            //Set the server certificate
            if (serverCertificate != null)
                this.Credentials.ServiceCertificate.Certificate = serverCertificate;

            //Set the client certificates and the validator
            if (string.IsNullOrEmpty(element.ClientCertificates) == false)
            {
                X509ClientCertificateAuthentication authentication = 
                            this.Credentials.ClientCertificate.Authentication;

                authentication.CertificateValidationMode =
                    System.ServiceModel.Security.X509CertificateValidationMode.Custom;

                authentication.CustomCertificateValidator =
                    new CustomCertificateValidator(element.GetClientCertificates());
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Davide Icardi
Software Developer
Italy Italy
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 30 Apr 2007
Article Copyright 2007 by Davide Icardi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid