Click here to Skip to main content
15,893,588 members
Articles / Web Development / ASP.NET

An easy way to use certificates for WCF security

Rate me:
Please Sign up or sign in to vote.
4.69/5 (38 votes)
30 Apr 2007MIT12 min read 477.7K   7.2K   136  
An easy solution to use certificates on Windows Communication Foundation loading the certificates from files
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace DevAge.ServiceModel.Configuration
{
    /*
     * 
     *  Here a typical configuration:
        <configSections>
            <!-- Add devage.serviceModel section handler -->
            <section name="devage.serviceModel" type="DevAge.ServiceModel.Configuration.Section, DevAge.ServiceModel" />
        </configSections>
        ...........................
     
        <!-- Specific DevAge.ServiceModel configuration used to set the client valid certificates and server certificate 
         This is a server configuration -->
        <devage.serviceModel>
            <!-- List of service type each with the relative server certificate and client certificate list -->
            <services>
              <add name="MathService" 
                    serverCertificate="App_Data\Server.pfx"
                    clientCertificates="App_Data\Client1.cer, App_Data\Client2.cer"
                    />
            </services>
        </devage.serviceModel>

         <!-- Specific DevAge.ServiceModel configuration used to set the client certificate 
         This is a client configuration -->
        <devage.serviceModel>
            <!-- List of endPoints each with the relative client certificate to use for authentication  -->
            <endPoints>
              <add contract="IMathService" 
                    clientCertificate="Client.pfx"
                    />
            </endPoints>
        </devage.serviceModel>
     * */

    /// <summary>
    /// The DevAge.ServiceModel conficuration section handler.
    /// Contains the list of certificates files to use.
    /// </summary>
    public class Section : ConfigurationSection
    {
        public static Section GetSection()
        {
            Section section =
                (Section)ConfigurationManager.GetSection("devage.serviceModel");

            return section;
        }

        [ConfigurationProperty("services")]
        public ServiceCollection Services
        {
            get { return this["services"] as ServiceCollection; }
        }

        [ConfigurationProperty("endPoints")]
        public EndPointCollection EndPoints
        {
            get { return this["endPoints"] as EndPointCollection; }
        }
    }
}

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


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions