Description
This section describes the CreateCertificate method of the SslStream class.
Syntax
C#
public static X509Certificate2 CreateCertificate(
string subjectName,
DateTime expirationDate,
string issuerName,
long serialNumber,
int keyStrength,
bool isExportable,
bool isSelfSigned,
bool includePublicKey,
SubjectKeyIdentifierType subjectKeyIdentifier
);
Parameters
- subjectName: The distinguished name (DN) of the certificate's subject.
- expirationDate: The date and time when the certificate expires.
- issuerName: The distinguished name (DN) of the certificate's issuer.
- serialNumber: The serial number of the certificate.
- keyStrength: The strength of the private key in bits.
- isExportable: A Boolean value that indicates whether the private key can be exported.
- isSelfSigned: A Boolean value that indicates whether the certificate is self-signed.
- includePublicKey: A Boolean value that indicates whether to include the public key in the certificate.
- subjectKeyIdentifier: An enum value that specifies how to generate the subject key identifier.
Returns
An X509Certificate2 object representing the generated certificate.
Remarks
The CreateCertificate method is a utility method that allows you to programmatically create an X509Certificate2 object. This is particularly useful for testing scenarios where you need to generate temporary certificates for SSL/TLS connections.
When creating a self-signed certificate, the issuerName should typically be the same as the subjectName.
Example
// Example of creating a self-signed certificate
DateTime expiration = DateTime.Now.AddYears(1);
string subject = "CN=MyTestServer, OU=MyOrg, O=MyCompany";
string issuer = "CN=MyTestServer, OU=MyOrg, O=MyCompany";
long serial = 1234567890;
int keyStrength = 2048;
X509Certificate2 cert = SslStream.CreateCertificate(
subject,
expiration,
issuer,
serial,
keyStrength,
true, // isExportable
true, // isSelfSigned
true, // includePublicKey
SubjectKeyIdentifierType.SubjectKeyIdentifier // or use other enum values
);
// Now you can use 'cert' for SSL/TLS communication
Requirements
Namespace: System.Net.Security
Assembly: System.dll