Click here to Skip to main content
12,451,093 members (48,096 online)
Click here to Skip to main content

Stats

325.2K views
6.1K 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.ServiceModel;
using System.ServiceModel.Description;
using System.Security.Cryptography.X509Certificates;

namespace DevAge.ServiceModel
{
    /// <summary>
    /// Client Proxy configurator
    /// </summary>
    public static class Proxy<T> where T : class
    {
        /// <summary>
        /// Configure the specified client proxy using the certificate file specified in the configuration file
        /// </summary>
        public static void Configure(ClientBase<T> client)
        {
            //Check if there is a valid configuration section
            Configuration.Section section = Configuration.Section.GetSection();
            if (section == null || section.EndPoints == null)
                return;

            //Check if there is a valid configuration for this service
            Configuration.EndPointElement element = section.EndPoints.GetElementByKey(client.Endpoint.Contract.ConfigurationName);
            if (element == null)
                return;

            X509Certificate2 certificate = element.GetClientCertificate();

            if (certificate != null)
                client.ClientCredentials.ClientCertificate.Certificate = certificate;
        }
    }
}

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...

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