WebVenta/WebVentaCoche/Services/VerificationService.cs
2024-12-17 14:57:49 +01:00

43 lines
1.2 KiB
C#

using Microsoft.Extensions.Caching.Memory;
namespace WebVentaCoche.Services
{
public class VerificationService
{
private readonly IMemoryCache _cache;
public VerificationService(IMemoryCache cache)
{
_cache = cache;
}
public void SaveVerificationCode(string email, int code, string token, TimeSpan expiration)
{
var cacheKey = GetCacheKey(email);
var data = (Code: code, Token: token, Expiration: DateTime.UtcNow.Add(expiration));
_cache.Set(cacheKey, data, expiration);
}
public (int Code, string Token, DateTime Expiration)? GetVerificationCode(string email)
{
var cacheKey = GetCacheKey(email);
if (_cache.TryGetValue(cacheKey, out (int Code, string Token, DateTime Expiration) data))
{
return data;
}
return null;
}
public void RemoveVerificationCode(string email)
{
var cacheKey = GetCacheKey(email);
_cache.Remove(cacheKey);
}
private string GetCacheKey(string email)
{
return $"Verification_{email}";
}
}
}