Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modify Sessions to allow support for non ASP.NET hosts

  • Loading branch information...
commit eae1cec11c7b583b4ce0cde200d76d0cc81c20da 1 parent 273774e
Demis Bellot mythz authored
2  src/ServiceStack.FluentValidation.Mvc3/Mvc/ServiceStackController.cs
View
@@ -59,7 +59,7 @@ public virtual ActionResult AuthorizationErrorResult
/// <summary>
/// Typed UserSession
/// </summary>
- protected object userSession;
+ private object userSession;
protected TUserSession SessionAs<TUserSession>()
{
return (TUserSession)(userSession ?? (userSession = Cache.SessionAs<TUserSession>()));
20 src/ServiceStack.ServiceInterface/Service.cs
View
@@ -92,25 +92,7 @@ public virtual ISession Session
private object userSession;
protected virtual TUserSession SessionAs<TUserSession>()
{
- if (userSession != null) return (TUserSession)userSession;
- if (SessionKey != null)
- userSession = Cache.Get<TUserSession>(SessionKey);
- else
- SessionFeature.CreateSessionIds();
- var unAuthorizedSession = typeof(TUserSession).CreateInstance();
- return (TUserSession)(userSession ?? (userSession = unAuthorizedSession));
- }
-
- /// <summary>
- /// The UserAgent's SessionKey
- /// </summary>
- protected virtual string SessionKey
- {
- get
- {
- var sessionId = SessionFeature.GetSessionId();
- return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId);
- }
+ return (TUserSession)(userSession ?? (userSession = Cache.SessionAs<TUserSession>(Request, Response)));
}
public virtual void Dispose()
2  src/ServiceStack.ServiceInterface/ServiceBase.cs
View
@@ -148,7 +148,7 @@ public ISession Session
private object userSession;
protected TUserSession SessionAs<TUserSession>()
{
- return (TUserSession)(userSession ?? (userSession = this.GetCacheClient().SessionAs<TUserSession>()));
+ return (TUserSession)(userSession ?? (userSession = this.GetCacheClient().SessionAs<TUserSession>(Request, Response)));
}
/// <summary>
26 src/ServiceStack.ServiceInterface/SessionExtensions.cs
View
@@ -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<TUserSession>(this ICacheClient cache)
+ public static TUserSession SessionAs<TUserSession>(this ICacheClient cache,
+ IHttpRequest httpReq = null, IHttpResponse httpRes = null)
{
- if (SessionKey != null)
+ var sessionKey = GetSessionKey(httpReq);
+
+ if (sessionKey != null)
{
- var userSession = cache.Get<TUserSession>(SessionKey);
+ var userSession = cache.Get<TUserSession>(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));
}
}
22 src/ServiceStack.ServiceInterface/SessionFeature.cs
View
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.