Skip to content

Commit

Permalink
Modify Sessions to allow support for non ASP.NET hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Oct 2, 2012
1 parent 273774e commit eae1cec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 43 deletions.
Expand Up @@ -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>()));
Expand Down
20 changes: 1 addition & 19 deletions src/ServiceStack.ServiceInterface/Service.cs
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceStack.ServiceInterface/ServiceBase.cs
Expand Up @@ -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>
Expand Down
26 changes: 13 additions & 13 deletions src/ServiceStack.ServiceInterface/SessionExtensions.cs
Expand Up @@ -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));
}

}
Expand Down
22 changes: 13 additions & 9 deletions src/ServiceStack.ServiceInterface/SessionFeature.cs
Expand Up @@ -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)
Expand Down

0 comments on commit eae1cec

Please sign in to comment.