Repository navigation
add redis cache demo for cookie session sample #824
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method seems overly complicated. Why not just generating an array of random bytes and encoding it to base64 or base64url to get a string key? |
||
| { | ||
| int maxSize = 8; | ||
| int size = maxSize; | ||
| var data = new byte[1]; | ||
| var crypto = new RNGCryptoServiceProvider(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, this my first PR, I will modify it and PR again |
||
| crypto.GetNonZeroBytes(data); | ||
| size = maxSize; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double assignment here 😊
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||
| 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(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "Data": { | ||
| "Redis": { | ||
| "ConnectionString": "localhost:6379,defaultDatabase=2" | ||
| } | ||
| }, | ||
| "Logging": { | ||
| "IncludeScopes": false, | ||
| "LogLevel": { | ||
| "Default": "Verbose", | ||
| "System": "Information", | ||
| "Microsoft": "Information" | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Redis and not just a DistributedCacheTicketStore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good, thanks