diff --git a/samples/CookieSessionSample/RedisCacheTicketStore.cs b/samples/CookieSessionSample/RedisCacheTicketStore.cs new file mode 100644 index 000000000..69fd24023 --- /dev/null +++ b/samples/CookieSessionSample/RedisCacheTicketStore.cs @@ -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 _ticketSerializer = TicketSerializer.Default; + + public RedisCacheTicketStore(RedisCacheOptions options) + { + _cache = new RedisCache(options); + } + + public async Task 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 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(); + } + } +} diff --git a/samples/CookieSessionSample/Startup.cs b/samples/CookieSessionSample/Startup.cs index ecb61ab66..f4b2140ac 100644 --- a/samples/CookieSessionSample/Startup.cs +++ b/samples/CookieSessionSample/Startup.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Caching.Redis; namespace CookieSessionSample { @@ -24,7 +25,10 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory) app.UseCookieAuthentication(new CookieAuthenticationOptions { AutomaticAuthenticate = true, - SessionStore = new MemoryCacheTicketStore() + //SessionStore = new MemoryCacheTicketStore() + SessionStore = new RedisTicketStore(new RedisCacheOptions { + Configuration = Configuration["Data:Redis:ConnectionString"], + }); }); app.Run(async context => diff --git a/samples/CookieSessionSample/appsettings.json b/samples/CookieSessionSample/appsettings.json new file mode 100644 index 000000000..ec1de84ee --- /dev/null +++ b/samples/CookieSessionSample/appsettings.json @@ -0,0 +1,15 @@ +{ + "Data": { + "Redis": { + "ConnectionString": "localhost:6379,defaultDatabase=2" + } + }, + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Verbose", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/samples/CookieSessionSample/project.json b/samples/CookieSessionSample/project.json index a3626218c..624f895ef 100644 --- a/samples/CookieSessionSample/project.json +++ b/samples/CookieSessionSample/project.json @@ -6,6 +6,7 @@ "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-*", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*", "Microsoft.Extensions.Caching.Memory": "1.0.0-*", + "Microsoft.Extensions.Caching.Redis": "1.0.0-*", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*" },