Jaahas.CertificateUtilities 1.1.0

dotnet add package Jaahas.CertificateUtilities --version 1.1.0                
NuGet\Install-Package Jaahas.CertificateUtilities -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Jaahas.CertificateUtilities" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Jaahas.CertificateUtilities --version 1.1.0                
#r "nuget: Jaahas.CertificateUtilities, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Jaahas.CertificateUtilities as a Cake Addin
#addin nuget:?package=Jaahas.CertificateUtilities&version=1.1.0

// Install Jaahas.CertificateUtilities as a Cake Tool
#tool nuget:?package=Jaahas.CertificateUtilities&version=1.1.0                

Jaahas.CertificateUtilties

This package provides a set of utilities for working with X.509 certificates.

Installation

Add a NuGet package reference to Jaahas.CertificateUtilities.

Getting Started

The package contains assemblies for both .NET Framework 4.7.2 and .NET 8.0. Some functionality is only available in the .NET 8.0 version.

Certificate Loader

The CertificateLoader class provides a simple way to load client or server certificates from the file system or from the Windows certificate store. It is largely based on Kestrel's internal certificate loader.

var loader = new CertificateLoader();

var certificateFromStore = loader.LoadCertificate(new CertificateLocation() {
    Subject = "MyCertificate",
    Store = "My",
    Location = "CurrentUser"
}, enhancedKeyUsage: CertificateLoader.ServerAuthenticationOid);

var certificateFromFile = loader.LoadCertificate(new CertificateLocation() {
    Path = @"C:\path\to\certificate.pfx",
    Password = "<PFX password>"
});

When loading a certificate from a certificate store you can also specify if expired or invalid certificates can be returned, and whether or not the private key for the certificate must also be available. By default, expired or invalid certificates are not returned and the private key is required.

var location = new CertificateLocation() {
    Subject = "MyCertificate",
    Store = "My",
    Location = "CurrentUser",
    AllowInvalid = true,
    RequirePrivateKey = false
};

You can also create a CertificateLocation instance from a path to a certificate file or certificate store location using the static CertificateLocation.CreateFromPath method. The method will parse your path to determine if it is a file system path or a certificate store path:

// Create from file path
var location1 = CertificateLocation.CreateFromPath(@"C:\path\to\certificate.pfx");

// Create from certificate store location
var location2 = CertificateLocation.CreateFromPath(@"cert:\CurrentUser\My\localhost");

Certificate store locations can be specified using the format cert:\{location}\{store}\{subject_or_thumbprint_or_distinguished_name}. The format is case-insensitive and can use both back- and forward-slashes as path separators.

When creating a CertificateLocation using CreateFromPath, remember that you may still need to set properties such as Password or KeyPath separately.

On .NET 8.0 and higher, it is also possible to load certificates and private keys from separate PEM-encoded files:

var loader = new CertificateLoader();

var certificate = loader.LoadCertificate(new CertificateLocation() {
    Path = @"C:\path\to\certificate.pem",
    KeyPath = @"C:\path\to\private-key.pem",
    Password = "<private key password>"
});
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 96 9/23/2024
1.0.0 112 9/20/2024
0.1.0 123 8/19/2024