From 7dc50fda260833d778ebee4817f1f7716b898d7c Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 20 Mar 2015 16:58:01 +1100 Subject: [PATCH] moves NaiveSessionCache to web proj --- src/Umbraco.Web.UI/Umbraco.Web.UI.csproj | 2 +- .../Security/Identity/NaiveSessionCache.cs | 84 +++++++++++++++++++ src/Umbraco.Web/Umbraco.Web.csproj | 1 + 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Umbraco.Web/Security/Identity/NaiveSessionCache.cs diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index 1392750d0c..4b8e33ca86 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -2609,7 +2609,7 @@ xcopy "$(ProjectDir)"..\packages\SqlServerCE.4.0.0.0\x86\*.* "$(TargetDir)x86\" True 7300 / - http://localhost:7300 + http://localhost:7301 False False diff --git a/src/Umbraco.Web/Security/Identity/NaiveSessionCache.cs b/src/Umbraco.Web/Security/Identity/NaiveSessionCache.cs new file mode 100644 index 0000000000..f4df1fbb30 --- /dev/null +++ b/src/Umbraco.Web/Security/Identity/NaiveSessionCache.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Web; +using Microsoft.IdentityModel.Clients.ActiveDirectory; + +namespace Umbraco.Web.Security.Identity +{ + //NOTE: Not sure exactly what this is for but it is found in the AD source demo: + // https://github.com/AzureADSamples/WebApp-WebAPI-OpenIDConnect-DotNet/blob/master/TodoListWebApp/Utils/NaiveSessionCache.cs + // apparently it is needed for AD auth, so we'll put it here for people to use. + // It would appear that this is better for whatever reason: https://github.com/OfficeDev/O365-WebApp-SingleTenant/blob/master/O365-WebApp-SingleTenant/Models/ADALTokenCache.cs + // and please note that that link came from finding this thread: https://twitter.com/chakkaradeep/status/544962341528285184 + + /// + /// This is required to initialize the AD Identity provider on startup + /// + public class NaiveSessionCache : TokenCache + { + private static readonly object FileLock = new object(); + readonly string _userObjectId = string.Empty; + readonly string _cacheId = string.Empty; + public NaiveSessionCache(string userId) + { + _userObjectId = userId; + _cacheId = _userObjectId + "_TokenCache"; + + this.AfterAccess = AfterAccessNotification; + this.BeforeAccess = BeforeAccessNotification; + Load(); + } + + public void Load() + { + lock (FileLock) + { + this.Deserialize((byte[])HttpContext.Current.Session[_cacheId]); + } + } + + public void Persist() + { + lock (FileLock) + { + // reflect changes in the persistent store + HttpContext.Current.Session[_cacheId] = this.Serialize(); + // once the write operation took place, restore the HasStateChanged bit to false + this.HasStateChanged = false; + } + } + + // Empties the persistent store. + public override void Clear() + { + base.Clear(); + System.Web.HttpContext.Current.Session.Remove(_cacheId); + } + + public override void DeleteItem(TokenCacheItem item) + { + base.DeleteItem(item); + Persist(); + } + + // Triggered right before ADAL needs to access the cache. + // Reload the cache from the persistent store in case it changed since the last access. + void BeforeAccessNotification(TokenCacheNotificationArgs args) + { + Load(); + } + + // Triggered right after ADAL accessed the cache. + void AfterAccessNotification(TokenCacheNotificationArgs args) + { + // if the access operation resulted in a cache update + if (this.HasStateChanged) + { + Persist(); + } + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 537f7dd6b1..79c59c85a4 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -560,6 +560,7 @@ +