Implementierung von JWTService und IJWTService für JWT-Operationen

Hinzugefügt wurden die JWTService-Klasse und das IJWTService-Interface zur Unterstützung der Generierung und Validierung von JWTs basierend auf Anspruchstypen. Enthält Methoden zum Erstellen von Sicherheitsschlüsseln, Generieren von Tokens und Lesen von Tokens mit ausführlicher Dokumentation.
This commit is contained in:
Developer 02
2024-04-15 13:52:59 +02:00
parent 79cf385a67
commit 49e3d29fd4
42 changed files with 1300 additions and 12 deletions

View File

@@ -0,0 +1,41 @@
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
namespace DigitalData.Core.Application
{
/// <summary>
/// Defines the operations for JWT service handling claims of type <typeparamref name="TClaimValue"/>.
/// </summary>
public interface IJWTService<TClaimValue>
{
/// <summary>
/// Generates a symmetric security key with the specified byte size.
/// </summary>
/// <param name="byteSize">The size of the security key in bytes. Default is 32 bytes.</param>
/// <returns>A new instance of <see cref="SymmetricSecurityKey"/>.</returns>
public static SymmetricSecurityKey GenerateSecurityKey(int byteSize = 32)
{
using var rng = RandomNumberGenerator.Create();
var randomBytes = new byte[byteSize];
rng.GetBytes(randomBytes);
var securityKey = new SymmetricSecurityKey(randomBytes);
return securityKey;
}
/// <summary>
/// Generates a token based on the specified claim value.
/// </summary>
/// <param name="claimValue">The claim value to encode in the token.</param>
/// <returns>A JWT as a string.</returns>
string GenerateToken(TClaimValue claimValue);
/// <summary>
/// Reads and validates a security token from a string representation.
/// </summary>
/// <param name="token">The JWT to read.</param>
/// <returns>A <see cref="JwtSecurityToken"/> if the token is valid; otherwise, null.</returns>
JwtSecurityToken? ReadSecurityToken(string token);
}
}