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.Security.Cryptography.X509Certificates;

namespace DevAge.ServiceModel
{
    static class CertificateHelper
    {
        /// <summary>
        /// Load a certificate from the specified file.
        /// The filename can contains the password when using this format: file|password.
        /// The file can be a relative file or an absolute file.
        /// Return null if the file is not specified.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static X509Certificate2 LoadFromFile(string file)
        {
            if (file != null)
                file = file.Trim();

            if (string.IsNullOrEmpty(file))
                return null;

            string[] parts = file.Split('|');
            if (parts.Length > 2)
                throw new ArgumentException("Certificate file name format not valid, but be in the format 'file|password'");

            string fullPath = PathHelper.LocateServerPath(parts[0].Trim());

            string password = string.Empty;
            if (parts.Length == 2)
                password = parts[1];

            return new X509Certificate2(fullPath, password);
        }
    }
}

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
Web03 | 2.8.1411023.1 | Last Updated 30 Apr 2007
Article Copyright 2007 by Davide Icardi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid