Using CAPICOM to obtain an SSL Certificate

Source: Internet
Author: User
Tags ssl certificate

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 participant 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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.