Click here to Skip to main content
12,348,925 members (61,166 online)
Click here to Skip to main content

Stats

319.4K views
6K downloads
131 bookmarked
Posted

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
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 EndPointElement : ConfigurationElement
    {
        /// <summary>
        /// Configuration name of the contract, usually is a complete type name of the contract interface (like Client.MathService.IMathService).
        /// </summary>
        [ConfigurationProperty("contract", IsRequired = true)]
        public string Contract
        {
            get { return (string)this["contract"]; }
            set { this["contract"] = value; }
        }

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

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

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

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((EndPointElement)element).Contract;
        }

        public EndPointElement GetElementByKey(string contractName)
        {
            foreach (EndPointElement service in this)
            {
                if (string.Equals(contractName, service.Contract, 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

You may also be interested in...

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