X509Certificate2.SubjectName Property
Assembly: System.Security.Cryptography.X509Certificates (in .NET Framework 4.5, .NET Core 1.0, .NET Standard 1.0, etc.)
Description
Gets the subject name of the X.509 certificate.
The subject name is a distinguished name (DN) that identifies the entity that is represented by the certificate.
Note: The SubjectName property returns an X500DistinguishedName object, which represents the subject name as a string.
Syntax
Property Value
An X500DistinguishedName object that represents the subject name of the certificate.
Remarks
The subject name is a fundamental part of an X.509 certificate, used to identify the entity (e.g., a server, a user, or an organization) to which the certificate is issued. This property provides access to that information in a structured format.
The X500DistinguishedName object provides methods to parse and format the distinguished name, allowing for detailed examination of its components, such as the common name (CN), organization (O), country (C), and more.
Example
The following example demonstrates how to retrieve and display the subject name from an X509Certificate2 object.
using namespace System.Net.Security;
using namespace System.Security.Cryptography.X509Certificates;
public class CertificateExample {
public static void Main(string[] args) {
// Assuming you have an X509Certificate2 object, for example, by loading from a file or store.
// In a real scenario, you would load your certificate here.
// For demonstration purposes, let's create a dummy certificate (this might not be valid for actual use).
X509Certificate2 cert = null;
try {
// Replace with a valid certificate path or store retrieval
// Example: cert = new X509Certificate2(@"C:\path\to\your\certificate.pfx", "password");
// If you don't have a real certificate, this part will fail.
// For testing, you might want to use a known self-signed certificate if available.
// If you have a certificate loaded:
if (cert != null) {
X500DistinguishedName subjectName = cert.SubjectName;
Console.WriteLine("Subject Name: {0}", subjectName.Name);
// You can also access individual name attributes
foreach (var rdn in subjectName.OidList) {
Console.WriteLine($" - {rdn.FriendlyName}: {subjectName.Name}"); // Note: This is a simplified example; proper parsing of RDNs is more complex.
}
} else {
Console.WriteLine("Certificate not loaded. Cannot display subject name.");
}
} catch (Exception ex) {
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine("Please ensure you have a valid certificate to test with.");
}
}
}
Serialization
This property is not directly serializable. The X500DistinguishedName object it returns can be converted to a string representation for serialization purposes.
Inheritance
This is a property of the X509Certificate2 class. It does not involve inheritance from other classes for this specific property.
Attributes
This property does not have any special attributes that affect its behavior beyond standard C# property definitions.
Exceptions
This property generally does not throw exceptions. However, exceptions might occur during the loading or initialization of the X509Certificate2 object itself, which could indirectly lead to issues if the certificate is invalid or inaccessible.