using System; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory; using Microsoft.Owin.Security; using Microsoft.Owin.Security.Cookies; namespace IwbZero.Zero.Configuration { public class IwbAuthenticationSessionStore : IAuthenticationSessionStore { private const string KeyPrefix = "IWB-"; private readonly IMemoryCache _cache; public IwbAuthenticationSessionStore() { _cache = new MemoryCache(new MemoryCacheOptions()); } public async Task StoreAsync(AuthenticationTicket ticket) { var key = KeyPrefix + Guid.NewGuid().ToString("N"); await RenewAsync(key, ticket); return key; } public Task RenewAsync(string key, AuthenticationTicket ticket) { var options = new MemoryCacheEntryOptions(); var expiresUtc = ticket.Properties.ExpiresUtc; if (expiresUtc.HasValue) { options.SetAbsoluteExpiration(expiresUtc.Value); } options.SetSlidingExpiration(TimeSpan.FromHours(1)); _cache.Set(key, ticket, options); return Task.FromResult(0); } public Task RetrieveAsync(string key) { _cache.TryGetValue(key, out AuthenticationTicket ticket); return Task.FromResult(ticket); } public Task RemoveAsync(string key) { _cache.Remove(key); return Task.FromResult(0); } } }