public static IAuthSession CreateNewSession(this IUserAuth user, IRequest httpReq) { var sessionId = SessionExtensions.CreateRandomSessionId(); var newSession = SessionFeature.CreateNewSession(httpReq, sessionId); var session = HostContext.AppHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession; session.PopulateSession(user); return(session); }
public static IAuthSession GetSession(this IRequest httpReq, bool reload = false) { if (httpReq == null) { return(null); } if (HostContext.TestMode) { var mockSession = httpReq.TryResolve <IAuthSession>(); //testing if (mockSession != null) { return(mockSession); } } object oSession = null; if (!reload) { httpReq.Items.TryGetValue(Keywords.Session, out oSession); } var sessionId = httpReq.GetSessionId(); var session = oSession as IAuthSession; if (session != null) { session = HostContext.AppHost.OnSessionFilter(session, sessionId); } if (session != null) { return(session); } var sessionKey = SessionFeature.GetSessionKey(sessionId); if (sessionKey != null) { session = httpReq.GetCacheClient().Get <IAuthSession>(sessionKey); if (session != null) { session = HostContext.AppHost.OnSessionFilter(session, sessionId); } } if (session == null) { var newSession = SessionFeature.CreateNewSession(httpReq, sessionId); session = HostContext.AppHost.OnSessionFilter(newSession, sessionId) ?? newSession; } httpReq.Items[Keywords.Session] = session; return(session); }
public static IAuthSession GetSession(this IRequest httpReq, bool reload = false) { if (httpReq == null) { return(null); } if (HostContext.TestMode) { var mockSession = httpReq.TryResolve <IAuthSession>(); //testing if (mockSession != null) { return(mockSession); } } object oSession = null; if (!reload) { httpReq.Items.TryGetValue(SessionFeature.RequestItemsSessionKey, out oSession); } var sessionId = httpReq.GetSessionId(); var cachedSession = FilterSession(oSession as IAuthSession, sessionId); if (cachedSession != null) { return(cachedSession); } using (var cache = httpReq.GetCacheClient()) { var sessionKey = SessionFeature.GetSessionKey(sessionId); var session = (sessionKey != null ? FilterSession(cache.Get <IAuthSession>(sessionKey), sessionId) : null) ?? SessionFeature.CreateNewSession(httpReq, sessionId); if (httpReq.Items.ContainsKey(SessionFeature.RequestItemsSessionKey)) { httpReq.Items.Remove(SessionFeature.RequestItemsSessionKey); } httpReq.Items.Add(SessionFeature.RequestItemsSessionKey, session); return(session); } }
public static IAuthSession GetSession(this IRequest httpReq, bool reload = false) { if (httpReq == null) { return(null); } if (HostContext.TestMode) { var mockSession = httpReq.TryResolve <IAuthSession>(); //testing if (mockSession != null) { return(mockSession); } } object oSession = null; if (!reload) { httpReq.Items.TryGetValue(Keywords.Session, out oSession); } if (oSession == null && !httpReq.Items.ContainsKey(Keywords.HasPreAuthenticated)) { try { HostContext.AppHost.ApplyPreAuthenticateFilters(httpReq, httpReq.Response); httpReq.Items.TryGetValue(Keywords.Session, out oSession); } catch (Exception ex) { Log.Error("Error in GetSession() when ApplyPreAuthenticateFilters", ex); /*treat errors as non-existing session*/ } } var sessionId = httpReq.GetSessionId(); var session = oSession as IAuthSession; if (session != null) { session = HostContext.AppHost.OnSessionFilter(httpReq, session, sessionId); } if (session != null) { return(session); } var sessionKey = SessionFeature.GetSessionKey(sessionId); if (sessionKey != null) { session = httpReq.GetCacheClient().Get <IAuthSession>(sessionKey); if (session != null) { session = HostContext.AppHost.OnSessionFilter(httpReq, session, sessionId); } } if (session == null) { var newSession = SessionFeature.CreateNewSession(httpReq, sessionId); session = HostContext.AppHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession; } httpReq.Items[Keywords.Session] = session; return(session); }
internal static async Task <IAuthSession> GetSessionInternalAsync(this IRequest httpReq, bool reload, bool async, CancellationToken token = default) { if (httpReq == null) { return(null); } if (HostContext.TestMode) { var mockSession = httpReq.TryResolve <IAuthSession>(); //testing if (mockSession != null) { return(mockSession); } } httpReq.Items.TryGetValue(Keywords.Session, out var oSession); if (reload && (oSession as IAuthSession)?.FromToken != true) // can't reload FromToken sessions from cache { oSession = null; } var appHost = HostContext.AppHost; if (oSession == null && !httpReq.Items.ContainsKey(Keywords.HasPreAuthenticated)) { try { await appHost.ApplyPreAuthenticateFiltersAsync(httpReq, httpReq.Response).ConfigAwait(); httpReq.Items.TryGetValue(Keywords.Session, out oSession); } catch (Exception ex) { Log.Error("Error in GetSession() when ApplyPreAuthenticateFilters", ex); /*treat errors as non-existing session*/ } } var sessionId = httpReq.GetSessionId(); var session = oSession as IAuthSession; if (session != null) { session = appHost.OnSessionFilter(httpReq, session, sessionId); } if (session != null) { return(session); } if (appHost.HasValidAuthSecret(httpReq)) { session = HostContext.GetAuthSecretSession(); if (session != null) { return(session); } } var sessionKey = SessionFeature.GetSessionKey(sessionId); if (sessionKey != null) { session = async ? await httpReq.GetCacheClientAsync().GetAsync <IAuthSession>(sessionKey, token).ConfigAwait() : httpReq.GetCacheClient().Get <IAuthSession>(sessionKey); if (session != null) { session = appHost.OnSessionFilter(httpReq, session, sessionId); } } if (session == null) { var newSession = SessionFeature.CreateNewSession(httpReq, sessionId); session = appHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession; } httpReq.Items[Keywords.Session] = session; return(session); }
public static IAuthSession GetSession(this IRequest request, bool reload = false) { if (HostContext.TestMode) { var mockSession = request.TryResolve <IAuthSession>(); //testing if (mockSession != null) { return(mockSession); } } object oSession = null; if (!reload) { request.Items.TryGetValue(Keywords.Session, out oSession); } // Apply PreAuthenticate Filters from IAuthWithRequest AuthProviders if (oSession == null && !request.Items.ContainsKey(Keywords.HasPreAuthenticated)) { request.Items[Keywords.HasPreAuthenticated] = true; foreach (var authProvider in AuthenticateService.AuthWithRequestProviders) { try { authProvider.PreAuthenticate(request, request.Response); } catch { } //throw new Exception("Error in GetSession() when ApplyPreAuthenticateFilters", ex); /*treat errors as non-existing session*/ } request.Items.TryGetValue(Keywords.Session, out oSession); } var sessionId = request.GetSessionId() ?? request.CreateSessionIds(request.Response); var session = oSession as IAuthSession; if (session != null) { session = HostContext.AppHost.OnSessionFilter(session, sessionId); } if (session != null) { return(session); } var sessionKey = SessionFeature.GetSessionKey(sessionId); if (sessionKey != null) { session = request.GetCacheClient().Get <IAuthSession>(sessionKey); if (session != null) { session = HostContext.AppHost.OnSessionFilter(session, sessionId); } } if (session == null) { var newSession = SessionFeature.CreateNewSession(request, sessionId); session = HostContext.AppHost.OnSessionFilter(newSession, sessionId) ?? newSession; } request.Items[Keywords.Session] = session; return(session); }