Getting Started with EricksonLopez.Security
L3StableNuGet: v1.0.0Local HEAD: v2.2.0AOT: Partial
Install EricksonLopez.Security, EricksonLopez.Security.Abstractions, and EricksonLopez.Security.Analyzers (1.0.0 on NuGet).
dotnet add package EricksonLopez.Security --version 1.0.0<PackageReference Include="EricksonLopez.Security" Version="1.0.0" /><PackageVersion Include="EricksonLopez.Security" Version="1.0.0" />dotnet add package EricksonLopez.Security.Abstractions --version 1.0.0dotnet add package EricksonLopez.Security.Analyzers --version 1.0.0dotnet add package EricksonLopez.Security.Network --version 1.0.0Publication Note:
EricksonLopez.Security.Mediatorexists only in local2.2.0HEAD and is not published on NuGet.org.
Quick Start: Key Activation & Tenant-Bound ISecretProtector
Section titled “Quick Start: Key Activation & Tenant-Bound ISecretProtector”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."); } }}