CertificateResource Class

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.

Syntax

public sealed class CertificateResource

Remarks

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.

Methods

Constructor

CertificateResource()

public CertificateResource()

Initializes a new instance of the CertificateResource class.

CertificateResource(string name, int value)

public CertificateResource(string name, int value)

Initializes a new instance of the CertificateResource class with the specified name and value.

  • name: The name of the certificate resource.
  • value: The integer value associated with the certificate resource.
  • Public Methods

    LoadCertificate(string path)

    public X509Certificate2 LoadCertificate(string path)

    Loads an X.509 certificate from the specified file path.

  • path: The full path to the certificate file.
  • Returns: An X509Certificate2 object representing the loaded certificate.

    GetThumbprint()

    public string GetThumbprint()

    Retrieves the thumbprint of the currently loaded certificate.

    Returns: A string containing the certificate's thumbprint.

    ValidateCertificate(X509Certificate2 certificate, string targetHost)

    public bool ValidateCertificate(X509Certificate2 certificate, string targetHost)

    Validates the provided X.509 certificate against the specified target host.

  • certificate: The X509Certificate2 object to validate.
  • targetHost: The host name for which the certificate is being validated.
  • Returns: true if the certificate is valid for the target host; otherwise, false.

    Properties

    Name

    public string Name { get; set; }

    Gets or sets the name of the certificate resource.

    Value

    public int Value { get; set; }

    Gets or sets the integer value associated with the certificate resource.

    Example Usage

    Loading and Validating a Certificate

    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);
            }
        }
    }

    See Also