/// <summary>
        /// Event handler for HttpApplication.AcquireRequestState
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        private void OnAcquireRequestState(object source, EventArgs args)
        {
            acquireCalled = true;

            HttpApplication app         = (HttpApplication)source;
            HttpContext     context     = app.Context;
            bool            isNew       = false;
            SessionItem     sessionData = null;
            bool            supportSessionIdReissue;

            sessionIdManager.InitializeRequest(context, false, out supportSessionIdReissue);
            var sessionId = sessionIdManager.GetSessionID(context);


            if (sessionId != null)
            {
                sessionData = GetSessionItem(sessionId);
            }
            else
            {
                bool redirected, cookieAdded;

                sessionId = sessionIdManager.CreateSessionID(context);
                sessionIdManager.SaveSessionID(context, sessionId, out redirected, out cookieAdded);

                if (redirected)
                {
                    return;
                }
            }

            if (sessionData == null)
            {
                // Identify the session as a new session state instance. Create a new SessionItem
                // and add it to the local Hashtable.

                isNew = true;

                sessionData = AddNewSessionItem(sessionId,
                                                SessionStateUtility.GetSessionStaticObjects(context));
            }

            // Add the session data to the current HttpContext.
            SessionStateUtility.AddHttpSessionStateToContext(context,
                                                             new HttpSessionStateContainer(sessionId,
                                                                                           sessionData.Items,
                                                                                           sessionData.StaticObjects,
                                                                                           Timeout,
                                                                                           isNew,
                                                                                           CookieMode,
                                                                                           SessionStateMode.Custom,
                                                                                           false));

            // Execute the Session_OnStart event for a new session.
            if (isNew && Start != null)
            {
                Start(this, EventArgs.Empty);
            }
        }
예제 #2
0
        private void OnAcquireRequestState(object source, EventArgs args)
        {
            HttpApplication app     = (HttpApplication)source;
            HttpContext     context = app.Context;
            bool            isNew   = false;
            string          sessionId;

            RedisSessionItemHash sessionItemCollection = null;
            bool supportSessionIDReissue = true;

            sessionIDManager.InitializeRequest(context, false, out supportSessionIDReissue);
            sessionId = sessionIDManager.GetSessionID(context);

            if (sessionId == null)
            {
                bool redirected, cookieAdded;

                sessionId = sessionIDManager.CreateSessionID(context);
                sessionIDManager.SaveSessionID(context, sessionId, out redirected, out cookieAdded);

                isNew = true;

                if (redirected)
                {
                    return;
                }
            }

            if (!RequiresSessionState(new HttpContextWrapper(context)))
            {
                return;
            }

            releaseCalled = false;

            sessionItemCollection = new RedisSessionItemHash(sessionId, redisConfig.SessionTimeout, GetRedisConnection());

            if (sessionItemCollection.Count == 0)
            {
                isNew = true;
            }

            // Add the session data to the current HttpContext.
            SessionStateUtility.AddHttpSessionStateToContext(context,
                                                             new TaskWaitRedisHttpSessionStateContainer(sessionId,
                                                                                                        sessionItemCollection,
                                                                                                        SessionStateUtility.GetSessionStaticObjects(context),
                                                                                                        redisConfig.SessionTimeout,
                                                                                                        isNew,
                                                                                                        redisConfig.CookieMode,
                                                                                                        SessionStateMode.Custom,
                                                                                                        false));

            // Execute the Session_OnStart event for a new session.
            if (isNew && Start != null)
            {
                Start(this, EventArgs.Empty);
            }
        }
예제 #3
0
        void OnAcquireRequestState(object o, EventArgs args)
        {
            Trace.WriteLine("SessionStateModule.OnAcquireRequestState (hash " + this.GetHashCode().ToString("x") + ")");
            HttpApplication application = (HttpApplication)o;
            HttpContext     context     = application.Context;

            if (!(context.Handler is IRequiresSessionState))
            {
                Trace.WriteLine("Handler (" + context.Handler + ") does not require session state");
                return;
            }
            bool isReadOnly = (context.Handler is IReadOnlySessionState);

            bool supportSessionIDReissue;

            if (idManager.InitializeRequest(context, false, out supportSessionIDReissue))
            {
                return;                 // Redirected, will come back here in a while
            }
            string sessionId = idManager.GetSessionID(context);

            handler.InitializeRequest(context);

            storeData = GetStoreData(context, sessionId, isReadOnly);

            storeIsNew = false;
            if (storeData == null && !storeLocked)
            {
                storeIsNew = true;
                sessionId  = idManager.CreateSessionID(context);
                Trace.WriteLine("New session ID allocated: " + sessionId);
                bool redirected;
                bool cookieAdded;
                idManager.SaveSessionID(context, sessionId, out redirected, out cookieAdded);
                if (redirected)
                {
                    if (supportSessionIDReissue)
                    {
                        handler.CreateUninitializedItem(context, sessionId, (int)config.Timeout.TotalMinutes);
                    }
                    context.Response.End();
                    return;
                }
                else
                {
                    storeData = handler.CreateNewStoreData(context, (int)config.Timeout.TotalMinutes);
                }
            }
            else if (storeData == null && storeLocked)
            {
                WaitForStoreUnlock(context, sessionId, isReadOnly);
            }
            else if (storeData != null &&
                     !storeLocked &&
                     storeSessionAction == SessionStateActions.InitializeItem &&
                     IsCookieLess(context, config))
            {
                storeData = handler.CreateNewStoreData(context, (int)config.Timeout.TotalMinutes);
            }

            container = CreateContainer(sessionId, storeData, storeIsNew, isReadOnly);
            SessionStateUtility.AddHttpSessionStateToContext(app.Context, container);
            if (storeIsNew)
            {
                OnSessionStart();
                HttpSessionState hss = app.Session;

                if (hss != null)
                {
                    storeData.Timeout = hss.Timeout;
                }
            }

            // Whenever a container is abandoned, we temporarily disable the expire call back.
            // So in this case we are quite sure we have a brand new container, so we make sure it works again.
            supportsExpiration = handler.SetItemExpireCallback(OnSessionExpired);
        }
예제 #4
0
        //</Snippet3>


        //<Snippet4>
        //
        // Event handler for HttpApplication.AcquireRequestState
        //

        private void OnAcquireRequestState(object source, EventArgs args)
        {
            HttpApplication app     = (HttpApplication)source;
            HttpContext     context = app.Context;
            bool            isNew   = false;
            string          sessionID;
            SessionItem     sessionData             = null;
            bool            supportSessionIDReissue = true;

            pSessionIDManager.InitializeRequest(context, false, out supportSessionIDReissue);
            sessionID = pSessionIDManager.GetSessionID(context);


            if (sessionID != null)
            {
                try
                {
                    pHashtableLock.AcquireReaderLock(Int32.MaxValue);
                    sessionData = (SessionItem)pSessionItems[sessionID];

                    if (sessionData != null)
                    {
                        sessionData.Expires = DateTime.Now.AddMinutes(pTimeout);
                    }
                }
                finally
                {
                    pHashtableLock.ReleaseReaderLock();
                }
            }
            else
            {
                bool redirected, cookieAdded;

                sessionID = pSessionIDManager.CreateSessionID(context);
                pSessionIDManager.SaveSessionID(context, sessionID, out redirected, out cookieAdded);

                if (redirected)
                {
                    return;
                }
            }

            if (sessionData == null)
            {
                // Identify the session as a new session state instance. Create a new SessionItem
                // and add it to the local Hashtable.

                isNew = true;

                sessionData = new SessionItem();

                sessionData.Items         = new SessionStateItemCollection();
                sessionData.StaticObjects = SessionStateUtility.GetSessionStaticObjects(context);
                sessionData.Expires       = DateTime.Now.AddMinutes(pTimeout);

                try
                {
                    pHashtableLock.AcquireWriterLock(Int32.MaxValue);
                    pSessionItems[sessionID] = sessionData;
                }
                finally
                {
                    pHashtableLock.ReleaseWriterLock();
                }
            }

            // Add the session data to the current HttpContext.
            SessionStateUtility.AddHttpSessionStateToContext(context,
                                                             new HttpSessionStateContainer(sessionID,
                                                                                           sessionData.Items,
                                                                                           sessionData.StaticObjects,
                                                                                           pTimeout,
                                                                                           isNew,
                                                                                           pCookieMode,
                                                                                           SessionStateMode.Custom,
                                                                                           false));

            // Execute the Session_OnStart event for a new session.
            if (isNew && Start != null)
            {
                Start(this, EventArgs.Empty);
            }
        }
        private async Task AcquireStateAsync(HttpContext context)
        {
            _acquireCalled = true;
            _releaseCalled = false;
            ResetPerRequestFields();

            _rqContext = new HttpContextWrapper(context);

            SessionEventSource.Log.SessionDataBegin(context);

            /* Notify the store we are beginning to get process request */
            _store.InitializeRequest(_rqContext);

            /* determine if the request requires state at all */
            _rqRequiresState = SessionStateUtility.IsSessionStateRequired(context);

            // SessionIDManager may need to do a redirect if cookieless setting is AutoDetect
            if (_idManager.InitializeRequest(context, false, out _rqSupportSessionIdReissue))
            {
                SessionEventSource.Log.SessionDataEnd(context);
                return;
            }

            /* Get sessionid */
            _rqId = _idManager.GetSessionID(context);
            if (!_rqRequiresState)
            {
                if (_rqId != null)
                {
                    // Still need to update the sliding timeout to keep session alive.
                    // There is a plan to skip this for perf reason.  But it was postponed to
                    // after Whidbey.
                    await _store.ResetItemTimeoutAsync(_rqContext, _rqId, GetCancellationToken());
                }

                SessionEventSource.Log.SessionDataEnd(context);
                return;
            }

            // If the page is marked as DEBUG, HttpContext.Timeout will return a very large value (~1 year)
            // In this case, we want to use the executionTimeout value specified in the config to avoid
            // PollLockedSession to run forever.
            _rqExecutionTimeout = s_configExecutionTimeout;

            /* determine if we need just read-only access */
            _rqReadonly = SessionStateUtility.IsSessionStateReadOnly(context);

            if (_rqId != null)
            {
                /* get the session state corresponding to this session id */
                await GetSessionStateItemAsync();
            }
            else
            {
                /* if there's no id yet, create it */
                bool redirected = CreateSessionId();

                _rqIdNew = true;

                if (redirected)
                {
                    if (s_configRegenerateExpiredSessionId)
                    {
                        // See inline comments in CreateUninitializedSessionState()
                        await _store.CreateUninitializedItemAsync(_rqContext, _rqId, s_timeout, GetCancellationToken());
                    }

                    SessionEventSource.Log.SessionDataEnd(context);
                    return;
                }
            }

            await CompleteAcquireStateAsync(context);
        }