Egov.Extensions.Configuration
10.0.4
Prefix Reserved
dotnet add package Egov.Extensions.Configuration --version 10.0.4
NuGet\Install-Package Egov.Extensions.Configuration -Version 10.0.4
<PackageReference Include="Egov.Extensions.Configuration" Version="10.0.4" />
<PackageVersion Include="Egov.Extensions.Configuration" Version="10.0.4" />
<PackageReference Include="Egov.Extensions.Configuration" />
paket add Egov.Extensions.Configuration --version 10.0.4
#r "nuget: Egov.Extensions.Configuration, 10.0.4"
#:package Egov.Extensions.Configuration@10.0.4
#addin nuget:?package=Egov.Extensions.Configuration&version=10.0.4
#tool nuget:?package=Egov.Extensions.Configuration&version=10.0.4
Egov.Extensions.Configuration
A .NET library that provides helpers for certificate loading and configuring IOptions<SystemCertificateOptions> in ASP.NET Core 10.0+ applications. It serves as a shared foundation for certificate management in services built on the eGov platform.
Table of Contents
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Supported Certificate Formats
- Error Handling
- Testing
- Contributing
- Code of Conduct
- AI Assistance
- License
Features
- Load public certificates from CER/CRT files
- Load private certificates from PFX/PKCS#12 files
- Load certificates from Kubernetes TLS secrets (
tls.crt/tls.key) - Load full certificate chains including intermediate certificates
- Register certificates via ASP.NET Core Dependency Injection
TypeConvertersupport for binding certificates directly from configuration strings
Prerequisites
- .NET 10.0 or later
- A valid certificate file (PFX, CER, or PEM format)
- Certificate password if the certificate is password-protected
- For Kubernetes deployments: certificates mounted as TLS secrets with
tls.crtandtls.keyfiles
Installation
Install the package from NuGet:
dotnet add package Egov.Extensions.Configuration
Or via the Package Manager Console:
Install-Package Egov.Extensions.Configuration
Configuration
Add the following section to your appsettings.json:
{
"Certificate": {
"Path": "Files/Certificates/your-certificate.pfx",
"Password": "your-certificate-password"
}
}
Path can be:
- A path to a PFX file (requires
Passwordif encrypted) - A path to a CER/CRT file (public certificate only)
- A path to a directory containing
tls.crtandtls.keyfiles (Kubernetes mounted secret)
Password is optional and only required for encrypted PFX files or encrypted PEM private keys.
Usage
Using Dependency Injection (Recommended)
Register the certificate in Program.cs:
builder.Services.AddSystemCertificate(builder.Configuration.GetSection("Certificate"));
Or configure it inline:
builder.Services.AddSystemCertificate(options =>
{
options.Path = "Files/Certificates/your-certificate.pfx";
options.Password = "your-certificate-password";
});
Then inject IOptions<SystemCertificateOptions> into your services:
public class MyService
{
private readonly SystemCertificateOptions _certificateOptions;
public MyService(IOptions<SystemCertificateOptions> certificateOptions)
{
_certificateOptions = certificateOptions.Value;
}
public void UseCertificate()
{
var certificate = _certificateOptions.Certificate;
// Use the certificate...
}
}
The SystemCertificateOptions also exposes IntermediateCertificates when the loaded certificate chain contains intermediate certificates.
Using CertificateLoader Directly
You can use the CertificateLoader static class without dependency injection.
Load a public certificate (CER/CRT):
var certificate = CertificateLoader.Public("path/to/certificate.cer");
Load a private certificate (PFX):
var certificate = CertificateLoader.Private("path/to/certificate.pfx", "password");
Load from a Kubernetes mounted secret:
var certificate = CertificateLoader.Private("/etc/ssl/certs/my-secret", null);
Load a full chain (certificate + intermediates):
var certificate = CertificateLoader.PrivateChain(
"path/to/certificate.pfx",
"password",
out X509Certificate2Collection? intermediates);
Using CertificateConverter
CertificateConverter is a TypeConverter that allows binding an X509Certificate2 directly from a configuration string.
Register it once at application startup:
CertificateConverter.Register();
After registration, configuration strings are automatically converted:
"path/to/cert.cer"→ loads a public certificate"path/to/cert.pfx|password"→ loads a private certificate (path and password separated by|)
Supported Certificate Formats
| Format | Description |
|---|---|
| PFX / PKCS#12 | Password-protected certificate bundle with private key |
| CER / CRT | Public certificate file (DER or PEM encoded) |
| PEM | Certificate and key pair (tls.crt + tls.key), used with Kubernetes TLS secrets |
Error Handling
| Scenario | Exception |
|---|---|
| Path is null or empty | ArgumentException |
| File or directory not found | ArgumentException |
| No certificate with private key found in PFX | ArgumentException |
| Certificate could not be loaded (missing path and no direct assignment) | InvalidOperationException |
| Loaded certificate does not contain a private key | InvalidOperationException |
Testing
The solution includes a dedicated test project Egov.Extensions.Configuration.Tests built with xUnit v3.
Test coverage
| Test class | What is covered |
|---|---|
CertificateLoaderTests |
CertificateLoader.Public, Private, and PrivateChain — happy paths, null/empty/missing paths, PEM+key via directory |
CertificateConverterTests |
CertificateConverter type conversion from config strings (CER path, PFX path with \| separator), error cases |
SystemCertificateOptionsTests |
Default property values, property assignment, Dispose safety (null cert, double-dispose, intermediate certificates) |
SystemCertificateOptionsPostConfigureTests |
Post-configure pipeline: loads certificate from path, validates private key presence, propagates intermediates |
SystemCertificateExtensionsTests |
AddSystemCertificate DI extension — section binding and inline Action<> overload |
Test certificates
The test project ships self-signed test certificates under TestCertificates/:
| File | Purpose |
|---|---|
egov_library_test.pfx |
Password-protected PKCS#12 bundle (password: test) |
egov_library_test.cer |
Public certificate (PEM) |
egov_library_tes.key |
Private key (PEM) |
These files are automatically copied to the build output directory by the project file and are not intended for production use.
Running the tests
dotnet test src/Egov.Extensions.Configuration.Tests
Or from the solution root:
dotnet test
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines on how to get started.
Code of Conduct
This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.
AI Assistance
This repository contains an AGENTS.md file with instructions and context for AI coding agents to assist in development, ensuring consistency in code style and project structure.
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Egov.Extensions.Configuration:
| Package | Downloads |
|---|---|
|
Egov.Integrations.MPass.Saml
This package contains an authentication handler for MPass implementing SAML 2.0 protocol for ASP.NET Core. |
|
|
Egov.Integrations.MSign.Soap
A reusable library to connect with MSign using SOAP protocol. |
|
|
Egov.Integrations.MConnect.Events
This package contains services for easy integration of producers and consumers with MConnect Events. |
|
|
Egov.Integrations.MNotify
A .NET client library for integrating with the MNotify service. |
|
|
Egov.Integrations.MDocs
A high-performance C# library for easy integration with MDocs document management services. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial public release.