How can I create a self-signed certificate using C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In modern .NET, the cleanest way to create a self-signed certificate in C# is to use CertificateRequest. That API lets you generate a key pair, define the subject and extensions, create the certificate, and export it as a PFX or CER file for development or internal testing.
What a Self-Signed Certificate Is
A self-signed certificate signs itself with its own private key instead of being signed by a trusted certificate authority. That makes it useful for:
- local HTTPS development
- internal testing
- temporary lab environments
It is not appropriate for public production endpoints where clients must trust the certificate automatically.
A Complete Example
The example below creates a self-signed certificate for localhost, valid for one year, and exports it as a PFX.
This is a practical baseline for local development.
Why SubjectAlternativeName Matters
Modern TLS clients care about the Subject Alternative Name extension more than the old common name field alone. If you want the certificate to work for localhost, an IP address, or a custom dev hostname, put those names in the SAN extension explicitly.
For example:
Without the correct SAN entries, the certificate may still be created successfully but fail hostname validation.
Export Formats
The two most common export choices are:
- '
X509ContentType.Pfxwhen you need the private key' - '
X509ContentType.Certwhen you only need the public certificate'
PFX is useful for local servers such as Kestrel or IIS development setups. CER is useful when distributing the public certificate separately.
Installing or Trusting the Certificate
Generating the certificate is only part of the job. For local HTTPS development, you may also need to trust it in the local certificate store or import it into the tool that will consume it.
That trust step is platform- and environment-specific, but the main idea is the same: clients must explicitly trust a self-signed certificate because no external authority vouches for it.
Common Pitfalls
The biggest mistake is thinking "self-signed" means "production-ready but cheaper." It does not. The trust model is completely different.
Another issue is forgetting the Subject Alternative Name extension. Many clients will reject certificates that rely only on the common name.
Developers also sometimes export only the public certificate when the server actually needs the private key too. In that case, you need a PFX.
Finally, be deliberate about key size, validity period, and storage. Even for local certificates, weak defaults and casual private-key handling are still bad habits.
Summary
- In modern C#, use
CertificateRequestto create self-signed certificates. - Add useful extensions such as key usage, subject key identifier, and Subject Alternative Name.
- Export as PFX when you need the private key, or CER when you only need the public certificate.
- Self-signed certificates are appropriate for development and internal testing, not for normal public production trust.
- Creating the certificate and trusting it are separate steps.

