Ir al contenido

Buscar en el Ecosistema EricksonLopez.*

Escribe el nombre de una librería, ID de paquete NuGet, tipo .NET o concepto arquitectónico para buscar en el portal.

Primeros Pasos con EricksonLopez.Security

Instala EricksonLopez.Security, EricksonLopez.Security.Abstractions y EricksonLopez.Security.Analyzers (1.0.0 en NuGet).

dotnet add package EricksonLopez.Security --version 1.0.0
Ventana de terminal
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

Nota de Publicación: EricksonLopez.Security.Mediator existe únicamente en el workspace local 2.2.0 y no está publicado en NuGet.org.

AddEricksonLopezSecurity() vive en namespace Microsoft.Extensions.DependencyInjection; y supera 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.");
}
}
}