Skip to content

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.

Getting Started with EricksonLopez.Security

Install EricksonLopez.Security, EricksonLopez.Security.Abstractions, and EricksonLopez.Security.Analyzers (1.0.0 on NuGet).

dotnet add package EricksonLopez.Security --version 1.0.0
Terminal window
dotnet add package EricksonLopez.Security.Abstractions --version 1.0.0
dotnet add package EricksonLopez.Security.Analyzers --version 1.0.0
dotnet add package EricksonLopez.Security.Network --version 1.0.0

Publication Note: EricksonLopez.Security.Mediator exists only in local 2.2.0 HEAD and is not published on NuGet.org.

AddEricksonLopezSecurity() resides in namespace Microsoft.Extensions.DependencyInjection; and passes ValidateOnBuild=true, ValidateScopes=true:

using System;
using System.Threading.Tasks;
using EricksonLopez.Result;
using EricksonLopez.Security.Abstractions.Cryptography;
using EricksonLopez.Security.Abstractions.KeyManagement;
using EricksonLopez.Security.Abstractions.Primitives;
using EricksonLopez.Security.Abstractions.Secrets;
using Microsoft.Extensions.DependencyInjection;
namespace Example.SecurityQuickStart;
public static class SecurityUsage
{
public static async Task RunAsync()
{
var services = new ServiceCollection();
services.AddEricksonLopezSecurity();
using var provider = services.BuildServiceProvider(
new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
var keyLifecycle = provider.GetRequiredService<IKeyLifecycleManager>();
var keyResult = await keyLifecycle.GenerateAndActivateKeyAsync(KeyPurpose.SecretProtection);
if (keyResult.IsFailure)
{
throw new InvalidOperationException(keyResult.Error.Message);
}
var protector = provider.GetRequiredService<ISecretProtector>();
var context = AuthenticatedContext.ForTenant("tenant-42");
Result<byte[]> encrypted = await protector.ProtectAsync(
"api-token-value"u8.ToArray(),
KeyPurpose.SecretProtection,
context);
if (encrypted.IsFailure)
{
throw new InvalidOperationException(encrypted.Error.Message);
}
Result<byte[]> decrypted = await protector.UnprotectAsync(encrypted.Value, context);
if (decrypted.IsFailure || !decrypted.Value.AsSpan().SequenceEqual("api-token-value"u8))
{
throw new InvalidOperationException("Secret roundtrip mismatch.");
}
}
}