コード例 #1
0
        /// <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
        private bool CreateSessionId()
        {
            // CreateSessionId should be called only if:
            Debug.Assert(_rqId == null ||                       // Session id isn't found in the request, OR
                         (_rqSessionStateNotFound &&            // The session state isn't found, AND
                          s_configRegenerateExpiredSessionId && // We are regenerating expired session id, AND
                          _rqSupportSessionIdReissue &&         // This request supports session id re-issue, AND
                          !_rqIdNew),                           // The above three condition should imply the session id
                                                                // isn't just created, but is sent by the request.
                         "CreateSessionId should be called only if we're generating new id, or re-generating expired one");

            bool redirected;

            _rqId = _idManager.CreateSessionID(_rqContext.ApplicationInstance.Context);
            _idManager.SaveSessionID(_rqContext.ApplicationInstance.Context, _rqId, out redirected, out _rqAddedCookie);

            return(redirected);
        }
コード例 #4
0
ファイル: SessionStateModule.cs プロジェクト: Tsalex71/mono-1
        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);
        }
コード例 #5
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);
            }
        }