From eae1cec11c7b583b4ce0cde200d76d0cc81c20da Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Mon, 1 Oct 2012 23:59:16 -0400 Subject: [PATCH] Modify Sessions to allow support for non ASP.NET hosts --- .../Mvc/ServiceStackController.cs | 2 +- src/ServiceStack.ServiceInterface/Service.cs | 20 +------------- .../ServiceBase.cs | 2 +- .../SessionExtensions.cs | 26 +++++++++---------- .../SessionFeature.cs | 22 +++++++++------- 5 files changed, 29 insertions(+), 43 deletions(-) diff --git a/src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs b/src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs index 7c38f1f9736..4dc7a07d0c4 100644 --- a/src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs +++ b/src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs @@ -59,7 +59,7 @@ public virtual ActionResult AuthorizationErrorResult /// /// Typed UserSession /// - protected object userSession; + private object userSession; protected TUserSession SessionAs() { return (TUserSession)(userSession ?? (userSession = Cache.SessionAs())); diff --git a/src/ServiceStack.ServiceInterface/Service.cs b/src/ServiceStack.ServiceInterface/Service.cs index 5e7aa7795c8..486f883df67 100644 --- a/src/ServiceStack.ServiceInterface/Service.cs +++ b/src/ServiceStack.ServiceInterface/Service.cs @@ -92,25 +92,7 @@ public virtual ISession Session private object userSession; protected virtual TUserSession SessionAs() { - if (userSession != null) return (TUserSession)userSession; - if (SessionKey != null) - userSession = Cache.Get(SessionKey); - else - SessionFeature.CreateSessionIds(); - var unAuthorizedSession = typeof(TUserSession).CreateInstance(); - return (TUserSession)(userSession ?? (userSession = unAuthorizedSession)); - } - - /// - /// The UserAgent's SessionKey - /// - protected virtual string SessionKey - { - get - { - var sessionId = SessionFeature.GetSessionId(); - return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId); - } + return (TUserSession)(userSession ?? (userSession = Cache.SessionAs(Request, Response))); } public virtual void Dispose() diff --git a/src/ServiceStack.ServiceInterface/ServiceBase.cs b/src/ServiceStack.ServiceInterface/ServiceBase.cs index 8c3293c6583..fa960596055 100644 --- a/src/ServiceStack.ServiceInterface/ServiceBase.cs +++ b/src/ServiceStack.ServiceInterface/ServiceBase.cs @@ -148,7 +148,7 @@ public ISession Session private object userSession; protected TUserSession SessionAs() { - return (TUserSession)(userSession ?? (userSession = this.GetCacheClient().SessionAs())); + return (TUserSession)(userSession ?? (userSession = this.GetCacheClient().SessionAs(Request, Response))); } /// diff --git a/src/ServiceStack.ServiceInterface/SessionExtensions.cs b/src/ServiceStack.ServiceInterface/SessionExtensions.cs index b6857e98529..8a39f99214f 100644 --- a/src/ServiceStack.ServiceInterface/SessionExtensions.cs +++ b/src/ServiceStack.ServiceInterface/SessionExtensions.cs @@ -159,34 +159,34 @@ public static IHttpResponse ToResponse(this HttpListenerResponse listenerHttpRes return new HttpListenerResponseWrapper(listenerHttpRes); } - public static string SessionKey + public static string GetSessionKey(IHttpRequest httpReq = null) { - get - { - var sessionId = SessionFeature.GetSessionId(); - return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId); - } + var sessionId = SessionFeature.GetSessionId(httpReq); + return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId); } - public static TUserSession SessionAs(this ICacheClient cache) + public static TUserSession SessionAs(this ICacheClient cache, + IHttpRequest httpReq = null, IHttpResponse httpRes = null) { - if (SessionKey != null) + var sessionKey = GetSessionKey(httpReq); + + if (sessionKey != null) { - var userSession = cache.Get(SessionKey); + var userSession = cache.Get(sessionKey); if (!Equals(userSession, default(TUserSession))) return userSession; } - if (SessionKey == null) - SessionFeature.CreateSessionIds(); + if (sessionKey == null) + SessionFeature.CreateSessionIds(httpReq, httpRes); var unAuthorizedSession = (TUserSession)typeof(TUserSession).CreateInstance(); return unAuthorizedSession; } - public static void ClearSession(this ICacheClient cache) + public static void ClearSession(this ICacheClient cache, IHttpRequest httpReq = null) { - cache.Remove(SessionKey); + cache.Remove(GetSessionKey(httpReq)); } } diff --git a/src/ServiceStack.ServiceInterface/SessionFeature.cs b/src/ServiceStack.ServiceInterface/SessionFeature.cs index 58c15991677..c01203723d4 100644 --- a/src/ServiceStack.ServiceInterface/SessionFeature.cs +++ b/src/ServiceStack.ServiceInterface/SessionFeature.cs @@ -39,24 +39,28 @@ public static void AddSessionIdToRequestFilter(IHttpRequest req, IHttpResponse r } } - public static string GetSessionId() + public static string GetSessionId(IHttpRequest httpReq = null) { - if (HttpContext.Current == null) + if (httpReq == null && HttpContext.Current == null) throw new NotImplementedException(OnlyAspNet); - return HttpContext.Current.Request.ToRequest().GetSessionId(); + httpReq = httpReq ?? HttpContext.Current.Request.ToRequest(); + + return httpReq.GetSessionId(); } - public static void CreateSessionIds() + public static void CreateSessionIds(IHttpRequest httpReq = null, IHttpResponse httpRes = null) { - if (HttpContext.Current != null) + if (httpReq == null || httpRes == null) { - HttpContext.Current.Response.ToResponse() - .CreateSessionIds(HttpContext.Current.Request.ToRequest()); - return; + if (HttpContext.Current == null) + throw new NotImplementedException(OnlyAspNet); } - throw new NotImplementedException(OnlyAspNet); + httpReq = httpReq ?? HttpContext.Current.Request.ToRequest(); + httpRes = httpRes ?? HttpContext.Current.Response.ToResponse(); + + httpRes.CreateSessionIds(httpReq); } public static string GetSessionKey(string sessionId)