|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.