SelfCert: Create a Self-Signed Certificate Interactively (GUI) or Programmatically in .NET

While this isn’t new, I needed a new home for it since my old Pluralsight blog is gone now. Hopefully you’ll find it helpful!

It’s a bit of a pain to create self-signed certs using MAKECERT. So here’s a GUI-based tool that uses a combination of the .NET Framework and the CryptoAPI to create self-signed X.509 certificates. And it’s factored so that you can use the underlying library standalone – you can easily create certs programmatically now.

Here’s the GUI:

The GUI has some nifty features: you can create a PFX file directly, or you can save directly to a cert store of your choice. When you save to a cert store, an extra dialog pops up showing you where the private key file resides, so that you can adjust the ACL accordingly. I’ve got a “view private key” feature that launches explorer with the /select argument, taking you to the private key file so that you can set the ACL on it. Anyway, this extra dialog gives you some quick info you typically want, like the thumbprint. And there are buttons for browsing the cert store and viewing the certificate as well from here.

The GUI gens the RSA key pair on a background thread, so a) the app doesn’t lock up on you, and b) if you get tired of waiting for the key to gen, you can cancel easily enough :)

Here’s some code that does this programmatically by calling the Pluralsight.Crypto library that is underneath all of this. Those of you who are familiar with the CryptoAPI will recognize the key abstraction here, CryptContext.

static void GenSelfSignedCert()
{
    using (CryptContext ctx = new CryptContext())
    {
        ctx.Open();

        X509Certificate2 cert = ctx.CreateSelfSignedCertificate(
            new SelfSignedCertProperties
            {
                IsPrivateKeyExportable = true,
                KeyBitLength = 4096,
                Name = new X500DistinguishedName("cn=localhost"),
                ValidFrom = DateTime.Today.AddDays(-1),
                ValidTo = DateTime.Today.AddYears(1),
            });

        X509Certificate2UI.DisplayCertificate(cert);
    }
}

Make sure you’ve got the Microsoft .NET Framework 3.5 installed. Self-Cert relies on it.

Download the project here, which includes binaries and sources. Feel free to use Pluralsight.Crypto in your own projects if you find it useful. Enjoy!

25 thoughts on “SelfCert: Create a Self-Signed Certificate Interactively (GUI) or Programmatically in .NET

  1. Hi,

    Pretty useful tool. Is there any Pluralsight course that explains about x 509 certificates and discusses the various options within them?I have been looking for such a tutorial for a long time now.

  2. Very nice tool. With source code it’s awesome! Thanks.
    BTW I have a question. Did you know how to set the friendly name property in the self signed certified? I realized that in IIS, the generated certified doesn’t have one.

  3. Pingback: What tools can I use to generate X.509 certificates? | PHP Developer Resource

  4. Pingback: Cert issue setting up WCF in IIS | Jisku.com - Developers Network

  5. Pingback: WCF Service with message based security validating against AspNet Membership Provider « Shawson's Code Blog

  6. Pingback: Servicio WCF expuesto con REST y SOAP « Jameson López .NET

  7. Sorry but when i try to assign the .pfx file to “Sign the assembly” option, it pops up password dialog box and i have not choosen to enter password while creating .pfx. what is going wrong here?

  8. Giving following Error, while i try to publish 1 Cannot import the following key file: April.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_020C12985435E179

  9. hi
    need to create ssl certificate with multiple purpose (client authentication && server authentication).Is it possible.

  10. Hi,
    let me first thank you, i guess this is what i am searching for since a while, i’m coding my first TCP client for training purposes , in VB2010 with .Net4, and was wondering from where to get these stuff , what are they and so on…i am using SslStream , is that helping to set client certificates to connect with a certified server ?
    great work , thank you

    • It’s been quite awhile since I’ve used it, but as I recall, SslStream is a great way to secure a TCP connection, and SelfCert should help you create a certificate that you can use with that type of solution. Good luck!

  11. Pingback: Integrating your windows store app with https nodejs web service | Building Apps @ Program Thee world

  12. Thanks for the tool. It must have saved me couple of hours to generate a self signed certificate for localhost. In https client/server scenario, the localhost certificate needs to be imported in trusted cas on the client machine for the scenario to work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s