Last active
June 5, 2016 19:34
-
-
Save casper-rasmussen/358f98d4ee800edcbbde42febe334f85 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// <summary> | |
/// Implementation that stores MVC TempData in DDS instead of through session | |
/// </summary> | |
class DynamicDataStoreTempDataProvider : ITempDataProvider | |
{ | |
private readonly ILog _logManager; | |
public DynamicDataStoreTempDataProvider() | |
{ | |
this._logManager = LogManager.GetLogger(typeof(DynamicDataStoreTempDataProvider)); | |
} | |
private Guid GetUserId(HttpContextBase context) | |
{ | |
//Get Anonymous ID if user isn't authenticated | |
if (!context.User.Identity.IsAuthenticated) | |
{ | |
if (AnonymousIdentificationModule.Enabled) | |
return new Guid(context.Request.AnonymousID); | |
else | |
return Guid.NewGuid(); | |
} | |
//Get the current users membership | |
MembershipUser membership = Membership.GetUser(); | |
if (membership == null) | |
return Guid.NewGuid(); | |
Guid guid; | |
if (membership.ProviderUserKey == null) | |
return Guid.NewGuid(); | |
//Let's check if its a guid we can use | |
if (Guid.TryParse(membership.ProviderUserKey.ToString(), out guid)) | |
return guid; | |
//Get a Guid if it is a int | |
if (membership.ProviderUserKey is Int32) | |
{ | |
byte[] bytes = new byte[16]; | |
int userId = (int) membership.ProviderUserKey; | |
BitConverter.GetBytes(userId).CopyTo(bytes, 0); | |
Guid membershipUserGuid = new Guid(bytes); | |
return membershipUserGuid; | |
} | |
return Guid.NewGuid(); | |
} | |
public void SaveTempData(ControllerContext controllerContext, IDictionary<string, object> values) | |
{ | |
Guid userId = this.GetUserId(controllerContext.HttpContext); | |
if (values == null || values.Count == 0) | |
{ | |
TempDataStoreItem tempDataStoreItem = this.GetStoreItem(userId); | |
//Remove the item from our store | |
if (tempDataStoreItem != null) | |
this.RemoveStoreItem(userId); | |
return; | |
} | |
BinaryFormatter bf = new BinaryFormatter(); | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
try | |
{ | |
//Serialize our data to store | |
bf.Serialize(ms, values); | |
byte[] raw = ms.ToArray(); | |
TempDataStoreItem tempDataStoreItem = new TempDataStoreItem(); | |
tempDataStoreItem.Id = Identity.NewIdentity(userId); | |
tempDataStoreItem.RawTempData = raw; | |
//Save it | |
this.SaveStoreItem(tempDataStoreItem); | |
} | |
catch (SerializationException e) | |
{ | |
this._logManager.Error("Failed to serialize values for TempData", e); | |
} | |
} | |
} | |
public IDictionary<string, object> LoadTempData(ControllerContext controllerContext) | |
{ | |
Guid userId = this.GetUserId(controllerContext.HttpContext); | |
//Get item for user | |
TempDataStoreItem tempDataStoreItem = this.GetStoreItem(userId); | |
if (tempDataStoreItem != null) | |
this.RemoveStoreItem(userId); | |
if (tempDataStoreItem == null || tempDataStoreItem.RawTempData == null) | |
return null; | |
BinaryFormatter bf = new BinaryFormatter(); | |
using (MemoryStream ms = new MemoryStream(tempDataStoreItem.RawTempData)) | |
{ | |
try | |
{ | |
//Deserialize | |
Dictionary<string, object> rawValues = bf.Deserialize(ms) as Dictionary<string, object>; | |
return rawValues; | |
} | |
catch (SerializationException e) | |
{ | |
this._logManager.Error("Failed to deserialize values for TempData", e); | |
return null; | |
} | |
} | |
} | |
private TempDataStoreItem GetStoreItem(Guid userId) | |
{ | |
using (DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(TempDataStoreItem)) ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(TempDataStoreItem))) | |
{ | |
if (store == null) | |
throw new InvalidOperationException("could not get datastore for tempDataStoreItem"); | |
TempDataStoreItem tempDataStoreItem = store.Load<TempDataStoreItem>(Identity.NewIdentity(userId)); | |
return tempDataStoreItem; | |
} | |
} | |
public void RemoveStoreItem(Guid userId) | |
{ | |
using (DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(TempDataStoreItem)) ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(TempDataStoreItem))) | |
{ | |
if (store == null) | |
throw new InvalidOperationException("Could not get datastore for TempDataStoreItem"); | |
store.Delete(Identity.NewIdentity(userId)); | |
} | |
} | |
public TempDataStoreItem SaveStoreItem(TempDataStoreItem tempDataStoreItem) | |
{ | |
if (tempDataStoreItem == null) | |
throw new ArgumentNullException("tempDataStoreItem"); | |
using (DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(TempDataStoreItem)) ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(TempDataStoreItem))) | |
{ | |
if (store == null) | |
throw new InvalidOperationException("Could not get datastore for TempDataStoreItem"); | |
store.Save(tempDataStoreItem); | |
} | |
return tempDataStoreItem; | |
} | |
} | |
[EPiServerDataStore(AutomaticallyCreateStore = true, AutomaticallyRemapStore = true, StoreName = "TempData.DynamicDataStoreTempDataProvider")] | |
class TempDataStoreItem : IDynamicData | |
{ | |
public Identity Id { get; set; } | |
public byte[] RawTempData { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment