Repository navigation
Expand file tree
/
Copy pathRedisCacheTicketStore.cs
More file actions
76 lines (64 loc) · 2.4 KB
/
Copy pathRedisCacheTicketStore.cs
File metadata and controls
76 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Redis;
namespace CookieSessionSample
{
public class RedisCacheTicketStore : ITicketStore
{
private static readonly char[] _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
private readonly IDistributedCache _cache;
private readonly IDataSerializer<AuthenticationTicket> _ticketSerializer = TicketSerializer.Default;
public RedisCacheTicketStore(RedisCacheOptions options)
{
_cache = new RedisCache(options);
}
public async Task<string> StoreAsync(AuthenticationTicket ticket)
{
var key = GetUniqueKey();
await RenewAsync(key, ticket);
return key;
}
public async Task RenewAsync(string key, AuthenticationTicket ticket)
{
var options = new DistributedCacheEntryOptions();
var expiresUtc = ticket.Properties.ExpiresUtc;
if (expiresUtc.HasValue) {
options.SetAbsoluteExpiration(expiresUtc.Value);
}
options.SetSlidingExpiration(TimeSpan.FromHours(1)); // TODO: configurable.
var data = _ticketSerializer.Serialize(ticket);
await _cache.SetAsync(key, data, options);
}
public async Task<AuthenticationTicket> RetrieveAsync(string key)
{
var data = await _cache.GetAsync(key);
var ticket = _ticketSerializer.Deserialize(data);
return ticket;
}
public async Task RemoveAsync(string key)
{
await _cache.RemoveAsync(key);
}
private static string GetUniqueKey()
{
int maxSize = 8;
int size = maxSize;
var data = new byte[1];
var crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
var sb = new StringBuilder(size);
foreach (byte b in data) {
sb.Append(_chars[b % (_chars.Length - 1)]);
}
return sb.ToString();
}
}
}