Hi all,
Iam trying to do some certificate management using CAPICOM
lib. Iam writing this website with a backend c#. All i want is know is
how to select the website on IIS to do certificate installation and
viewing. That is currently when i run my program from the ASP .NET dev
server it installs and views the certificates present in "Default Web
Site" of IIS Manager console. Lets say if i run a new website "xyz",
how can i do certificate management for that.
The is below.
public Certificates ShowCertificates()
{
string Info;
Store localMachineCertStore = new Store();
localMachineCertStore.Open(CAPICOM_STORE_LOCATION. CAPICOM_LOCAL_MACHINE_STORE,
"My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ON LY);
Certificates certificates =
(Certificates)localMachineCertStore.Certificates;
return certificates;
}
public void InstallCertificate(Certificate SelectedCert)
{
byte[] thumbprintByteArray = null;
string thumbprint = SelectedCert.Thumbprint;
MSAdminBaseClass adminBaseClass = new MSAdminBaseClass();
UtilitiesClass Utils = new UtilitiesClass();
string binaryThumbprint = Utils.HexToBinary(thumbprint);
thumbprintByteArray =
(byte[])Utils.BinaryStringToByteArray(binaryThumbprint);
adminBaseClass.SetMetabaseData(SslCertHashId, metaDataPath,
thumbprintByteArray);
adminBaseClass.SetMetabaseData(SslStoreNameId,
metaDataPath, "MY");
}
public void SetMetabaseData(uint metabaseDataId, string
metabaseDataPath, object data)
{
if(data == null)
throw new ArgumentNullException("data");
// Create METADATA_RECORD
METADATA_RECORD metaDataRecord = new METADATA_RECORD();
metaDataRecord.dwMDIdentifier = metabaseDataId;
metaDataRecord.dwMDAttributes =
(UInt32)METADATA_ATTRIBUTES.METADATA_INHERIT;
metaDataRecord.dwMDUserType =
(UInt32)METADATA_USER_TYPE.IIS_MD_UT_SERVER;
try
{
// Open MetaData Key
IntPtr metaDataKeyHandle = IntPtr.Zero;
try
{
IntPtr metaDataMasterRootHandle = new
IntPtr(METADATA_MASTER_ROOT_HANDLE);
adminBaseInterface.OpenKey(
metaDataMasterRootHandle,
"/LM",
(METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE),
20,
out metaDataKeyHandle);
if(metaDataKeyHandle == IntPtr.Zero)
throw new ExternalException("Error occured opening IIS
Metabase!");
#region Copy MetaData to Unmanaged Memory
if(data is System.String)
{
string stringData = data as String;
// Set MetaData Record Type
metaDataRecord.dwMDDataType =
(UInt32)METADATA_TYPES.STRING_METADATA;
// Allocate String Data Memory (Add null terminated)
stringData += '\0';
metaDataRecord.dwMDDataLen =
(UInt32)Encoding.Unicode.GetByteCount(stringData);
metaDataRecord.pbMDData =
Marshal.StringToCoTaskMemUni(stringData);
if(metaDataRecord.pbMDData == IntPtr.Zero)
{
throw new Exception("Unable to allocate string data buffer for
Metabase entry.");
}
}
else if(data is System.Array)
{
byte[] binaryData = data as Byte[];
if(binaryData != null)
{
// Set MetaData Record Type
metaDataRecord.dwMDDataType =
(UInt32)METADATA_TYPES.BINARY_METADATA;
// Allocate Binary Data Memory
metaDataRecord.dwMDDataLen = (UInt32)binaryData.Length;
metaDataRecord.pbMDData =
Marshal.AllocCoTaskMem(binaryData.Length);
if(metaDataRecord.pbMDData == IntPtr.Zero)
{
throw new Exception("Unable to allocate binary data buffer for
Metabase entry.");
}
// Copy Binary Data to Unmanaged Memory
Marshal.Copy(binaryData, 0, metaDataRecord.pbMDData,
(int)metaDataRecord.dwMDDataLen);
}
string[] stringArrayData = data as String[];
if(stringArrayData != null)
{
// Set MetaData Record Type
metaDataRecord.dwMDDataType =
(UInt32)METADATA_TYPES.MULTISZ_METADATA;
ArrayList multiSzData = new ArrayList();
foreach(string stringData in stringArrayData)
{
// (Add null terminated)
multiSzData.AddRange(Encoding.Unicode.GetBytes(str ingData +
'\0'));
}
// (Add null terminated)
multiSzData.AddRange(new byte[2]{0x00,0x00});
binaryData =
(byte[])multiSzData.ToArray(Type.GetType("System.Byte"));
// Allocate Binary Data Memory
metaDataRecord.dwMDDataLen = (UInt32)binaryData.Length;
metaDataRecord.pbMDData =
Marshal.AllocCoTaskMem(binaryData.Length);
// Copy Binary Data to Unmanaged Memory
Marshal.Copy(binaryData, 0, metaDataRecord.pbMDData,
(int)metaDataRecord.dwMDDataLen);
}
}
else if(data is System.UInt32)
{
int uintData = (int)data;
// Set MetaData Record Type
metaDataRecord.dwMDDataType =
(UInt32)METADATA_TYPES.DWORD_METADATA;
// Allocate DWORD Data Memory
metaDataRecord.dwMDDataLen =
(uint)Marshal.SizeOf(typeof(UInt32));
metaDataRecord.pbMDData =
Marshal.AllocCoTaskMem((int)metaDataRecord.dwMDDat aType);
Marshal.WriteInt32(metaDataRecord.pbMDData, uintData);
if(metaDataRecord.pbMDData == IntPtr.Zero)
{
throw new Exception("Unable to allocate DWORD data buffer for
Metabase entry.");
}
}
else if(data is System.Int32)
{
int intData = (int)data;
// Set MetaData Record Type
metaDataRecord.dwMDDataType =
(UInt32)METADATA_TYPES.DWORD_METADATA;
// Allocate DWORD Data Memory
metaDataRecord.dwMDDataLen =
(uint)Marshal.SizeOf(typeof(UInt32));
metaDataRecord.pbMDData =
Marshal.AllocCoTaskMem((int)metaDataRecord.dwMDDat aType);
Marshal.WriteInt32(metaDataRecord.pbMDData, intData);
if(metaDataRecord.pbMDData == IntPtr.Zero)
{
throw new Exception("Unable to allocate DWORD data buffer for
Metabase entry.");
}
}
#endregion Copy MetaData to Unmanaged Memory
// Set MetaData
adminBaseInterface.SetData(metaDataKeyHandle, metabaseDataPath,
ref metaDataRecord);
}
finally
{
// Close Key
adminBaseInterface.CloseKey(metaDataKeyHandle);
// Save Data
adminBaseInterface.SaveData();
}
}
finally
{
// Free Unmanaged Resources
if(metaDataRecord.pbMDData != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(metaDataRecord.pbMDData);
}
}
}
}
Please help.
Varun