예제 #1
0
        void OnReleaseRequestState(object o, EventArgs args)
        {
            Trace.WriteLine("SessionStateModule.OnReleaseRequestState (hash " + this.GetHashCode().ToString("x") + ")");

            HttpApplication application = (HttpApplication)o;
            HttpContext     context     = application.Context;

            if (!(context.Handler is IRequiresSessionState))
            {
                return;
            }

            Trace.WriteLine("\tsessionId == " + container.SessionID);
            Trace.WriteLine("\trequest path == " + context.Request.FilePath);
            Trace.WriteLine("\tHandler (" + context.Handler + ") requires session state");
            try
            {
                if (!container.IsAbandoned)
                {
                    Trace.WriteLine("\tnot abandoned");
                    if (!container.IsReadOnly)
                    {
                        Trace.WriteLine("\tnot read only, storing and releasing");
                        handler.SetAndReleaseItemExclusive(context, container.SessionID, storeData, storeLockId, storeIsNew);
                    }
                    else
                    {
                        Trace.WriteLine("\tread only, releasing");
                        handler.ReleaseItemExclusive(context, container.SessionID, storeLockId);
                    }
                    handler.ResetItemTimeout(context, container.SessionID);
                }
                else
                {
                    handler.ReleaseItemExclusive(context, container.SessionID, storeLockId);
                    handler.RemoveItem(context, container.SessionID, storeLockId, storeData);
                    if (supportsExpiration)
#if TARGET_J2EE
                    {; }
                    else
#else
                    {// Make sure the expiration handler is not called after we will have raised
                     // the session end event.
                        handler.SetItemExpireCallback(null);
                    }
#endif
                    { SessionStateUtility.RaiseSessionEnd(container, this, args); }
                }
                SessionStateUtility.RemoveHttpSessionStateFromContext(context);
            }
            finally
            {
                container = null;
                storeData = null;
            }
        }
예제 #2
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);
        }
예제 #3
0
        public void Init(HttpApplication app)
        {
            config = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");

            ProviderSettings settings;

            switch (config.Mode)
            {
            case SessionStateMode.Custom:
                settings = config.Providers [config.CustomProvider];
                if (settings == null)
                {
                    throw new HttpException(String.Format("Cannot find '{0}' provider.", config.CustomProvider));
                }
                break;

            case SessionStateMode.Off:
                return;

            case SessionStateMode.InProc:
                settings = new ProviderSettings(null, typeof(SessionInProcHandler).AssemblyQualifiedName);
                break;

            case SessionStateMode.SQLServer:
                settings = new ProviderSettings(null, typeof(SessionSQLServerHandler).AssemblyQualifiedName);
                break;

            case SessionStateMode.StateServer:
                settings = new ProviderSettings(null, typeof(SessionStateServerHandler).AssemblyQualifiedName);
                break;

            default:
                throw new NotImplementedException(String.Format("The mode '{0}' is not implemented.", config.Mode));
            }

            handler = (SessionStateStoreProviderBase)ProvidersHelper.InstantiateProvider(settings, typeof(SessionStateStoreProviderBase));

            if (String.IsNullOrEmpty(config.SessionIDManagerType))
            {
                idManager = new SessionIDManager();
            }
            else
            {
                Type idManagerType = HttpApplication.LoadType(config.SessionIDManagerType, true);
                idManager = (ISessionIDManager)Activator.CreateInstance(idManagerType);
            }

            try {
                idManager.Initialize();
            } catch (Exception ex) {
                throw new HttpException("Failed to initialize session ID manager.", ex);
            }

            supportsExpiration = handler.SetItemExpireCallback(OnSessionExpired);
            HttpRuntimeSection runtime = HttpRuntime.Section;

            executionTimeout = runtime.ExecutionTimeout;
            //executionTimeoutMS = executionTimeout.Milliseconds;

            this.app = app;

            app.BeginRequest        += new EventHandler(OnBeginRequest);
            app.AcquireRequestState += new EventHandler(OnAcquireRequestState);
            app.ReleaseRequestState += new EventHandler(OnReleaseRequestState);
            app.EndRequest          += new EventHandler(OnEndRequest);
        }
예제 #4
0
		public void Init (HttpApplication app)
		{
			config = (SessionStateSection) WebConfigurationManager.GetSection ("system.web/sessionState");

			ProviderSettings settings;
			switch (config.Mode) {
				case SessionStateMode.Custom:
					settings = config.Providers [config.CustomProvider];
					if (settings == null)
						throw new HttpException (String.Format ("Cannot find '{0}' provider.", config.CustomProvider));
					break;
				case SessionStateMode.Off:
					return;
				case SessionStateMode.InProc:
					settings = new ProviderSettings (null, typeof (SessionInProcHandler).AssemblyQualifiedName);
					break;

				case SessionStateMode.SQLServer:
					settings = new ProviderSettings (null, typeof (SessionSQLServerHandler).AssemblyQualifiedName);
					break;

				case SessionStateMode.StateServer:
					settings = new ProviderSettings (null, typeof (SessionStateServerHandler).AssemblyQualifiedName);
					break;

				default:
					throw new NotImplementedException (String.Format ("The mode '{0}' is not implemented.", config.Mode));
			
			}

			handler = (SessionStateStoreProviderBase) ProvidersHelper.InstantiateProvider (settings, typeof (SessionStateStoreProviderBase));

			if (String.IsNullOrEmpty(config.SessionIDManagerType)) {
				idManager = new SessionIDManager ();
			} else {
				Type idManagerType = HttpApplication.LoadType (config.SessionIDManagerType, true);
				idManager = (ISessionIDManager)Activator.CreateInstance (idManagerType);
			}

			try {				
				idManager.Initialize ();
			} catch (Exception ex) {
				throw new HttpException ("Failed to initialize session ID manager.", ex);
			}

			supportsExpiration = handler.SetItemExpireCallback (OnSessionExpired);
			HttpRuntimeSection runtime = HttpRuntime.Section;
			executionTimeout = runtime.ExecutionTimeout;
			//executionTimeoutMS = executionTimeout.Milliseconds;

			this.app = app;

			app.BeginRequest += new EventHandler (OnBeginRequest);
			app.AcquireRequestState += new EventHandler (OnAcquireRequestState);
			app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
			app.EndRequest += new EventHandler (OnEndRequest);
		}