Using CAPICOM to obtain an SSL Certificate
2007-08-16 17:32
369 查看
using System;
using System.Collections;
using System.Security.Cryptography.x509Certificates;
using interop.capicom;
namespace CAPIComWrapper
{
/// <summary>
/// Provides methods to interact with Windows Certificate stores.
/// </summary>
public class CertificateManager
{
/// <summary>
/// Searches for and returns a particular X509 certificate.
/// </summary>
/// <param name="SearchString">A full or partial certificate name</param>
/// <returns>An instance of the X509Certificate class.</returns>
public static X509Certificate Get(string SearchString)
{
string storeName = "My"; // "My" indicates the .Default store
StoreClass oStore;
Certificates oCerts;
X509Certificate foundcert = null; System;
// get a reference to the LOCAL MACHINE certificate store
oStore = new StoreClass();
oStore.Open(
CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE,
storeName,
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY);
// Get a list of all certificates in the store
oCerts = (Certificates)oStore.Certificates;
// get a list of only the matching certificates
oCerts = (Certificates)oCerts.Find(
CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME,
SearchString,
false);
// do we have any certs?
if(oCerts.Count > 0)
{
// reference the first certificate
Certificate firstcert = (Certificate)oCerts[1] ;
// get a certificate context from that cert
ICertContext iCertCntxt = (ICertContext) firstcert;
// now get a pointer to the context
int certcntxt = iCertCntxt.CertContext ;
// turn the int pointer into a managed IntPtr
IntPtr hCertCntxt = new IntPtr(certcntxt);
// was all of this successful?
if(hCertCntxt != IntPtr.Zero)
{
// create an X509Certificate from the cert context
foundcert = new X509Certificate(hCertCntxt);
}
// free the certificate context
iCertCntxt.FreeContext(certcntxt);
}
else
{
foundcert = null;
}
return foundcert;
}
}
}
using System.Collections;
using System.Security.Cryptography.x509Certificates;
using interop.capicom;
namespace CAPIComWrapper
{
/// <summary>
/// Provides methods to interact with Windows Certificate stores.
/// </summary>
public class CertificateManager
{
/// <summary>
/// Searches for and returns a particular X509 certificate.
/// </summary>
/// <param name="SearchString">A full or partial certificate name</param>
/// <returns>An instance of the X509Certificate class.</returns>
public static X509Certificate Get(string SearchString)
{
string storeName = "My"; // "My" indicates the .Default store
StoreClass oStore;
Certificates oCerts;
X509Certificate foundcert = null; System;
// get a reference to the LOCAL MACHINE certificate store
oStore = new StoreClass();
oStore.Open(
CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE,
storeName,
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY);
// Get a list of all certificates in the store
oCerts = (Certificates)oStore.Certificates;
// get a list of only the matching certificates
oCerts = (Certificates)oCerts.Find(
CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME,
SearchString,
false);
// do we have any certs?
if(oCerts.Count > 0)
{
// reference the first certificate
Certificate firstcert = (Certificate)oCerts[1] ;
// get a certificate context from that cert
ICertContext iCertCntxt = (ICertContext) firstcert;
// now get a pointer to the context
int certcntxt = iCertCntxt.CertContext ;
// turn the int pointer into a managed IntPtr
IntPtr hCertCntxt = new IntPtr(certcntxt);
// was all of this successful?
if(hCertCntxt != IntPtr.Zero)
{
// create an X509Certificate from the cert context
foundcert = new X509Certificate(hCertCntxt);
}
// free the certificate context
iCertCntxt.FreeContext(certcntxt);
}
else
{
foundcert = null;
}
return foundcert;
}
}
}
相关文章推荐
- How to sign an IIS SSL certificate request using OpenSSL
- Has anybody found a way to load HTTPS pages with an invalid server certificate using UIWebView?
- Moving an IIS SSL certificate to a Domino Keyring File[转]
- MySQL not using SSL the verifyServerCertificate property is set to 'false'
- how to create an ssl certificate on nginx for ubuntu14.04
- 转-Quick guide to setting up SSL using Domino as the Certificate Authority
- How to create jigsaw puzzle from an image using javascript(拼图引擎)
- Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
- Applications using Launch Screen Files and targetting iOS 7.1 and earlier need to also include a Launch Image in an Asset Catalog.
- Using Oracle Data Pump to create an external table
- git clone出现fatal: unable to access 'https://': SSL certificate problem: self signed certificate in c
- The certificate used to sign "xxx" has either expired or has been revoked. An updated certifica
- SSL rtificate problem: unable to get local issuer certificate
- how do i using c# to obtain call stack on crash?
- Using SharePoint Web Services to Fill Out an InfoPath Form
- Android - WebView 加载 Https 出现 SSL Error. Failed to validate the certificate chain
- How to make NAnt send an email using a real account
- <<How to setup an iSCSI target using tgtadm>>
- How to obtain an IBM JVM heapdump
- Using .NET Databinding to Bind an Editable InkPicture Control to a Database