Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions samples/CookieSessionSample/RedisCacheTicketStore.cs
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

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good, thanks

{
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RNGCryptoServiceProvider will only work on Windows and Mono. Consider replacing it by RandomNumberGenerator.Create() to get a x-plat'-friendly RNG.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double assignment here 😊

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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();
}
}
}
6 changes: 5 additions & 1 deletion samples/CookieSessionSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Redis;

namespace CookieSessionSample
{
Expand All @@ -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 =>
Expand Down
15 changes: 15 additions & 0 deletions samples/CookieSessionSample/appsettings.json
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"
}
}
}
1 change: 1 addition & 0 deletions samples/CookieSessionSample/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-*"
},
Expand Down