/// <summary>An IHttpRequest extension method that saves a session.</summary> /// /// <param name="httpReq"> The httpReq to act on.</param> /// <param name="session"> The session.</param> /// <param name="expiresIn">The expires in.</param> public static void SaveSession(this IHttpRequest httpReq, IAuthSession session, TimeSpan?expiresIn = null) { if (httpReq == null) { return; } using (var cache = httpReq.GetCacheClient()) { var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId()); cache.CacheSet(sessionKey, session, expiresIn ?? AuthFeature.GetDefaultSessionExpiry()); } httpReq.Items[RequestItemsSessionKey] = session; }
/// <summary>An IHttpRequest extension method that removes the session described by httpReq.</summary> /// /// <param name="httpReq">The httpReq to act on.</param> public static void RemoveSession(this IHttpRequest httpReq) { if (httpReq == null) { return; } using (var cache = httpReq.GetCacheClient()) { var sessionKey = SessionFeature.GetSessionKey(httpReq.GetSessionId()); cache.Remove(sessionKey); } httpReq.Items.Remove(RequestItemsSessionKey); }
/// <summary>An IHttpRequest extension method that gets a session.</summary> /// /// <param name="httpReq">The httpReq to act on.</param> /// <param name="reload"> true to reload.</param> /// /// <returns>The session.</returns> public static IAuthSession GetSession(this IHttpRequest httpReq, bool reload = false) { if (httpReq == null) { return(null); } object oSession = null; if (!reload) { httpReq.Items.TryGetValue(RequestItemsSessionKey, out oSession); } if (oSession != null) { return((IAuthSession)oSession); } using (var cache = httpReq.GetCacheClient()) { var sessionId = httpReq.GetSessionId(); var session = cache.Get <IAuthSession>(SessionFeature.GetSessionKey(sessionId)); if (session == null) { session = AuthService.CurrentSessionFactory(); session.Id = sessionId; session.CreatedAt = session.LastModified = DateTime.UtcNow; session.OnCreated(httpReq); } if (httpReq.Items.ContainsKey(RequestItemsSessionKey)) { httpReq.Items.Remove(RequestItemsSessionKey); } httpReq.Items.Add(RequestItemsSessionKey, session); return(session); } }
/// <summary>Gets session key.</summary> /// /// <param name="httpReq">The HTTP request.</param> /// /// <returns>The session key.</returns> public static string GetSessionKey(IHttpRequest httpReq = null) { var sessionId = SessionFeature.GetSessionId(httpReq); return(sessionId == null ? null : SessionFeature.GetSessionKey(sessionId)); }