Created
May 12, 2014 03:10
-
-
Save vibronet/90d4646c273930ff12d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Owin.Security; | |
using Microsoft.Owin.Security.Cookies; | |
using Microsoft.Owin.Security.DataHandler; | |
using System; | |
using System.Collections.Concurrent; | |
using System.ComponentModel.DataAnnotations; | |
using System.Data.Entity; | |
using System.Data.Entity.ModelConfiguration.Conventions; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace WebApp_WSFederation_DotNet | |
{ | |
public class NaiveEFAuthSessionEntry | |
{ | |
[Key] | |
public string Key { get; set; } | |
public string TicketString { get; set; } | |
} | |
public class NaiveEFAuthSessionStoreContext: DbContext | |
{ | |
public NaiveEFAuthSessionStoreContext(string init = "NaiveEFAuthSessionStoreContext") | |
: base(init) | |
{ } | |
public DbSet<NaiveEFAuthSessionEntry> Entries { get; set; } | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); | |
} | |
} | |
public class NaiveEFAuthSessionStoreInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<NaiveEFAuthSessionStoreContext> | |
{ | |
} | |
public class NaiveEFAuthSessionStore : IAuthenticationSessionStore | |
{ | |
private string _connectionString; | |
private TicketDataFormat _formatter; | |
private Timer _gcTimer; | |
public NaiveEFAuthSessionStore(TicketDataFormat tdf, string cns = "NaiveEFAuthSessionStoreContext") | |
{ | |
_connectionString = cns; | |
_formatter = tdf; | |
_gcTimer = new Timer(new TimerCallback(GarbageCollect), null, TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(15)); | |
} | |
private void GarbageCollect(object state) | |
{ | |
DateTimeOffset now = DateTimeOffset.Now.ToUniversalTime(); | |
using (NaiveEFAuthSessionStoreContext _store = new NaiveEFAuthSessionStoreContext(_connectionString)) | |
{ | |
foreach (var entry in _store.Entries) | |
{ | |
var expiresAt = _formatter.Unprotect(entry.TicketString).Properties.ExpiresUtc; | |
if (expiresAt < now) | |
{ | |
_store.Entries.Remove(entry); | |
} | |
} | |
_store.SaveChanges(); | |
} | |
} | |
public Task<string> StoreAsync(AuthenticationTicket ticket) | |
{ | |
using (NaiveEFAuthSessionStoreContext _store = new NaiveEFAuthSessionStoreContext(_connectionString)) | |
{ | |
string key = Guid.NewGuid().ToString(); | |
_store.Entries.Add(new NaiveEFAuthSessionEntry { Key = key, TicketString = _formatter.Protect(ticket) }); | |
_store.SaveChanges(); | |
return Task.FromResult(key); | |
} | |
} | |
public Task RenewAsync(string key, AuthenticationTicket ticket) | |
{ | |
using (NaiveEFAuthSessionStoreContext _store = new NaiveEFAuthSessionStoreContext(_connectionString)) | |
{ | |
NaiveEFAuthSessionEntry myEntry = _store.Entries.FirstOrDefault(a => a.Key == key); | |
if (myEntry != null) | |
{ | |
myEntry.TicketString = _formatter.Protect(ticket); | |
} | |
else | |
{ | |
_store.Entries.Add(new NaiveEFAuthSessionEntry { Key = key, TicketString = _formatter.Protect(ticket) }); | |
} | |
_store.SaveChanges(); | |
return Task.FromResult(0); | |
} | |
} | |
public Task<AuthenticationTicket> RetrieveAsync(string key) | |
{ | |
using (NaiveEFAuthSessionStoreContext _store = new NaiveEFAuthSessionStoreContext(_connectionString)) | |
{ | |
NaiveEFAuthSessionEntry myEntry = _store.Entries.FirstOrDefault(a => a.Key == key); | |
return Task.FromResult(_formatter.Unprotect(myEntry.TicketString)); | |
} | |
} | |
public Task RemoveAsync(string key) | |
{ | |
using (NaiveEFAuthSessionStoreContext _store = new NaiveEFAuthSessionStoreContext(_connectionString)) | |
{ | |
NaiveEFAuthSessionEntry myEntry = _store.Entries.FirstOrDefault(a => a.Key == key); | |
if (myEntry != null) | |
{ | |
_store.Entries.Remove(myEntry); | |
_store.SaveChanges(); | |
} | |
return Task.FromResult(0); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment