Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. public class MemoryCacheTicketStore : ITicketStore
  2. {
  3. private const string KeyPrefix = "AuthSessionStore-";
  4. private readonly IMemoryCache _cache;
  5.  
  6. public MemoryCacheTicketStore()
  7. {
  8. _cache = new MemoryCache(new MemoryCacheOptions());
  9. }
  10.  
  11. public async Task<string> StoreAsync(AuthenticationTicket ticket)
  12. {
  13. var guid = Guid.NewGuid();
  14. var key = KeyPrefix + guid.ToString();
  15. await RenewAsync(key, ticket);
  16. return key;
  17. }
  18.  
  19. public Task RenewAsync(string key, AuthenticationTicket ticket)
  20. {
  21. var options = new MemoryCacheEntryOptions();
  22. var expiresUtc = ticket.Properties.ExpiresUtc;
  23. if (expiresUtc.HasValue)
  24. {
  25. options.SetAbsoluteExpiration(expiresUtc.Value);
  26. }
  27. options.SetSlidingExpiration(TimeSpan.FromHours(1)); // TODO: configurable.
  28.  
  29. _cache.Set(key, ticket, options);
  30.  
  31. return Task.FromResult(0);
  32. }
  33.  
  34. public Task<AuthenticationTicket> RetrieveAsync(string key)
  35. {
  36. AuthenticationTicket ticket;
  37. _cache.TryGetValue(key, out ticket);
  38. return Task.FromResult(ticket);
  39. }
  40.  
  41. public Task RemoveAsync(string key)
  42. {
  43. _cache.Remove(key);
  44. return Task.FromResult(0);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement