Click here to Skip to main content
15,894,540 members
Articles / Web Development / IIS

WSE 3 Deployment: MSI and ClickOnce

Rate me:
Please Sign up or sign in to vote.
4.90/5 (61 votes)
4 Oct 2009CPOL29 min read 222K   776   217  
Overview of deployment techniques using example WSE 3 enabled solutions
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Security.AccessControl;
using System.Security.Principal;

namespace Util
{
    public class CertificateInstall
    {
        public static void PlaceInStore(X509Certificate2 cert,
            StoreName storeName, StoreLocation storeLocation, string user)
        {
            X509Store store = new X509Store(storeName, storeLocation);

            try
            {
                store.Open(OpenFlags.ReadWrite);

                if (!store.Certificates.Contains(cert))
                    store.Add(cert);

                int indexOfCert = store.Certificates.IndexOf(cert);
                X509Certificate2 certInStore = store.Certificates[indexOfCert];

                if (!string.IsNullOrEmpty(user))
                    AddAccessToCertificate(certInStore, user);
            }
            finally
            {
                store.Close();
            }
        }

        public static void RemoveFromStore(X509Certificate2 cert,
            StoreName storeName, StoreLocation storeLocation)
        {
            X509Store store = new X509Store(storeName, storeLocation);

            try
            {
                store.Open(OpenFlags.ReadWrite);

                if (store.Certificates.Contains(cert))
                    store.Remove(cert);
            }
            finally
            {
                store.Close();
            }
        }

        public static void RemoveFromStore(string name,
            StoreName storeName, StoreLocation storeLocation)
        {
            X509Store store = new X509Store(storeName, storeLocation);

            try
            {
                store.Open(OpenFlags.ReadWrite);

                foreach (X509Certificate2 cert in store.Certificates)
                {
                    if (cert.SubjectName.Name.StartsWith("CN=WSE2QuickStartServer"))
                    {
                        store.Remove(cert);
                        break;
                    }
                }
            }
            finally
            {
                store.Close();
            }
        }

        public static void AddAccessToCertificate(X509Certificate2 cert, string user)
        {
            RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider;

            if (rsa != null)
            {
                string keyfilepath =
                    FindKeyLocation(rsa.CspKeyContainerInfo.UniqueKeyContainerName);

                FileInfo file = new FileInfo(keyfilepath + "\\" +
                    rsa.CspKeyContainerInfo.UniqueKeyContainerName);

                FileSecurity fs = file.GetAccessControl();

                NTAccount account = new NTAccount(user);

                fs.AddAccessRule(new FileSystemAccessRule(account,
                    FileSystemRights.FullControl, AccessControlType.Allow));

                file.SetAccessControl(fs);
            }
        }

        private static string FindKeyLocation(string keyFileName)
        {
            string text1 =
            Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            string text2 = text1 + @"\Microsoft\Crypto\RSA\MachineKeys";
            string[] textArray1 = Directory.GetFiles(text2, keyFileName);
            if (textArray1.Length > 0)
            {
                return text2;
            }
            string text3 =
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string text4 = text3 + @"\Microsoft\Crypto\RSA\";
            textArray1 = Directory.GetDirectories(text4);
            if (textArray1.Length > 0)
            {
                foreach (string text5 in textArray1)
                {
                    textArray1 = Directory.GetFiles(text5, keyFileName);
                    if (textArray1.Length != 0)
                    {
                        return text5;
                    }
                }
            }
            return "Private key exists but is not accessible";
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Chief Technology Officer
United States United States
If you liked this article, consider reading other articles by me. For republishing article on other websites, please contact me by leaving a comment.

Comments and Discussions