Represents a resource for managing X.509 certificate information within a .NET application. This class provides methods and properties for accessing and manipulating certificate details, particularly useful in scenarios involving secure network communications.
The CertificateResource class is a fundamental component for developers working with SSL/TLS protocols and X.509 certificates in .NET. It abstracts the complexities of certificate handling, allowing for easier integration into applications requiring secure data transfer.
This class is often used in conjunction with other classes in the System.Net.Security namespace, such as SslStream, to establish secure communication channels.
Initializes a new instance of the CertificateResource class.
Initializes a new instance of the CertificateResource class with the specified name and value.
Loads an X.509 certificate from the specified file path.
Returns: An X509Certificate2 object representing the loaded certificate.
Retrieves the thumbprint of the currently loaded certificate.
Returns: A string containing the certificate's thumbprint.
Validates the provided X.509 certificate against the specified target host.
X509Certificate2 object to validate.Returns: true if the certificate is valid for the target host; otherwise, false.
Gets or sets the name of the certificate resource.
Gets or sets the integer value associated with the certificate resource.
using System; using System.Net.Security; using System.Security.Cryptography.X509Certificates; public class Example { public static void Main(string[] args) { // Specify the path to your certificate file string certificatePath = @"C:\certs\mycert.pfx"; string targetHostname = "www.example.com"; try { // Create an instance of CertificateResource CertificateResource certResource = new CertificateResource(); // Load the certificate X509Certificate2 certificate = certResource.LoadCertificate(certificatePath); // Set the certificate resource name (optional) certResource.Name = "MyWebServerCertificate"; certResource.Value = 12345; // Validate the certificate against a target host bool isValid = certResource.ValidateCertificate(certificate, targetHostname); if (isValid) { Console.WriteLine($"Certificate for {targetHostname} is valid."); Console.WriteLine($"Certificate Name: {certResource.Name}"); Console.WriteLine($"Certificate Thumbprint: {certResource.GetThumbprint()}"); } else { Console.WriteLine($"Certificate for {targetHostname} is NOT valid."); } } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } }