Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / VBScript

IIS Admin Base Object Wrapper for installing SSL Certificates

Rate me:
Please Sign up or sign in to vote.
4.44/5 (9 votes)
12 Feb 20046 min read 64.8K   1.5K   28  
A COM Interop wrapper for the IIS Admin Base Object that can be used to programmatically install SSL Certificates in IIS 5.0.
using System;
using CAPICOM;
using Windows.Services.Iis.Metabase;

namespace SslInstallSample
{
	class SslInstallSample
	{
		// IIS Schema Property IDs
		private const uint SslCertHashId =  5506;
		private const uint SslStoreNameId = 5511;
		// Default Web Site
		private const string metaDataPath = "/W3SVC/1";

		[STAThread]
		static void Main(string[] args)
		{
			
			// Open Personal Certificate Store "My"
			Store localMachineCertStore = new Store();
			localMachineCertStore.Open(CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE, "My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY);
			// Find Certificate
			Certificates certificates = (Certificates)localMachineCertStore.Certificates;
			try
			{
				certificates = certificates.Select("Certificate Selection", "Select a SSL Certificate for Internet Information Server", false);
			}
			catch(System.Runtime.InteropServices.COMException e)
			{
				if(e.ErrorCode == -2138568446)
				{
					Console.WriteLine(e.Message);
					return;
				}
			}
			// Get Thumbprint
			byte[] thumbprintByteArray = null;
			if(certificates.Count < 1)
			{
				Console.WriteLine("SSL Certificate not selected!");
				return;
			}
			else
			{
				// Indexer throws exception
				foreach(Certificate certificate in certificates)
				{
					// Get Hex String Thumbprint
					string thumbprint = certificate.Thumbprint;
					Console.WriteLine("SSL Certificate Thumbprint: " + thumbprint);
					// Convert Hex String to Byte[]
					Utilities certUtilities = new Utilities();
					string binaryThumbprint = certUtilities.HexToBinary(thumbprint);
					thumbprintByteArray = (byte[])certUtilities.BinaryStringToByteArray(binaryThumbprint);
				}
			}
			
			// Open Metabase Interface
			MSAdminBaseClass adminBaseClass = new MSAdminBaseClass();
			// Set SSL Certificate
			adminBaseClass.SetMetabaseData(SslCertHashId, metaDataPath, thumbprintByteArray);
			adminBaseClass.SetMetabaseData(SslStoreNameId, metaDataPath, "MY");
			Console.WriteLine("SSL Certificate successfully installed.");
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions