Click here to Skip to main content
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
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.Configuration;
using System.Security.Cryptography.X509Certificates;

namespace DevAge.ServiceModel.Configuration
{
    public class ServiceElement : ConfigurationElement
    {
        /// <summary>
        /// Type name of the service
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        /// <summary>
        /// Server certificate file (.pfx file).
        /// Can be an absolute or relative file.
        /// If the file require the password you can use this format: file|password .
        /// </summary>
        [ConfigurationProperty("serverCertificate", IsRequired = false)]
        public string ServerCertificate
        {
            get { return (string)this["serverCertificate"]; }
            set { this["serverCertificate"] = value; }
        }

        /// <summary>
        /// List of client certificates used for authentication.
        /// Each certificate file must be separated by a comma.
        /// </summary>
        [ConfigurationProperty("clientCertificates", IsRequired = false)]
        public string ClientCertificates
        {
            get { return (string)this["clientCertificates"]; }
            set { this["clientCertificates"] = value; }
        }

        public X509Certificate2 GetServerCertificate()
        {
            //Load the certificate from a file
            return CertificateHelper.LoadFromFile(ServerCertificate);
        }

        public IEnumerable<X509Certificate2> GetClientCertificates()
        {
            foreach (string file in ClientCertificates.Split(','))
            {
                string fullpath = file.Trim();

                if (string.IsNullOrEmpty(fullpath))
                    continue;

                fullpath = PathHelper.LocateServerPath(fullpath);

                //Load the certificate from a file
                X509Certificate2 certificate =
                        new X509Certificate2(fullpath, string.Empty);

                yield return certificate;
            }
        }
    }

    public class ServiceCollection : ConfigurationElementCollection
    {
        public ServiceElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as ServiceElement;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ServiceElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ServiceElement)element).Name;
        }

        public ServiceElement GetElementByKey(string serviceName)
        {
            foreach (ServiceElement service in this)
            {
                if (string.Equals(serviceName, service.Name, StringComparison.InvariantCultureIgnoreCase))
                    return service;
            }

            return null;
        }
    }
}

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

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