Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MemoryCacheTicketStore : ITicketStore
- {
- private const string KeyPrefix = "AuthSessionStore-";
- private readonly IMemoryCache _cache;
- public MemoryCacheTicketStore()
- {
- _cache = new MemoryCache(new MemoryCacheOptions());
- }
- public async Task<string> StoreAsync(AuthenticationTicket ticket)
- {
- var guid = Guid.NewGuid();
- var key = KeyPrefix + guid.ToString();
- 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)); // TODO: configurable.
- _cache.Set(key, ticket, options);
- return Task.FromResult(0);
- }
- public Task<AuthenticationTicket> RetrieveAsync(string key)
- {
- AuthenticationTicket ticket;
- _cache.TryGetValue(key, out ticket);
- return Task.FromResult(ticket);
- }
- public Task RemoveAsync(string key)
- {
- _cache.Remove(key);
- return Task.FromResult(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement