Files
2026-03-11 13:57:13 -04:00

44 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.Caching.Memory;
namespace maintenance.Authentication.Core;
public sealed class MemoryCacheTicketStore : ITicketStore
{
private const string KeyPrefix = "auth-ticket-";
private readonly IMemoryCache _cache;
private readonly TimeSpan _fallbackLifetime = TimeSpan.FromHours(8);
public MemoryCacheTicketStore(IMemoryCache cache)
{
_cache = cache;
}
public async Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = $"{KeyPrefix}{Guid.NewGuid():N}";
await RenewAsync(key, ticket);
return key;
}
public Task RenewAsync(string key, AuthenticationTicket ticket)
{
var expiration = ticket.Properties.ExpiresUtc?.UtcDateTime ?? DateTime.UtcNow.Add(_fallbackLifetime);
_cache.Set(key, ticket, expiration);
return Task.CompletedTask;
}
public Task<AuthenticationTicket?> RetrieveAsync(string key)
{
_cache.TryGetValue(key, out AuthenticationTicket? ticket);
return Task.FromResult(ticket);
}
public Task RemoveAsync(string key)
{
_cache.Remove(key);
return Task.CompletedTask;
}
}