SessionStateModule类
为一个应用程序提供session-state服务.这个类不能继承.
SessionStateModle 是ASP.NET的默认Sssion-state Handler,它写入session 数据并且从session-state存储中找回它和调用Session_OnStart和Session_OnEnd事件.下面详细关于怎样使用ASP.NET session state 去存储和为一个用户session找回值.
你能自定义实现IHttpModule接口取代SessionStateModule.
(1)IHttpModule Interface
提供module初始化和配置事件去实现类.
(2)SessionStateUtility Class
提供由session-state modules和session-state储存提供者提供去管理ASP.NET应用程序Session信息helper方法.
注意
SessionStateUtility类提供static helper方法,这个方法被一个session-state module所使用或一个session-state存储提供者.应用程序开发者不需要使用他们的代码调用这些方法.
下面是session-state module和session-state 储存者使用的方法:
(3)SessionStateUtility.GetHttpSessionStateFromContext Method
从当前请求context中找回session data.
Context参数为HttpContext
返回值类型为System.Web.SessionState.IHttpSessionState.
从当前请求中的Session 数据迁移到IHttpSessionState实现实例.
GetHttpSessionStateFromContext 方法能被一个session-state module使用来从当前请求中找回session data.在触发RelesseRequestState期间是在在一个请求结束时.返回session数据,之后被写入session数据存储.如果Session已经被废弃,这个session数据能从数据存储中被移出和HttpContext,并且Session_OnEnd时间就被执行.
注意:
你能使用RemoveHttpSessionStateFromContext方法去移出session 数据,并且使用 RaiseSessionEnd调用Session_OnEnd事件.
(4)SessionStateUtility.AddHttpSessionStateToContext Method
从当前请求context里申请session数据.
参数:
context:就是HttpContext.
container:类型为IHttpSessionState,实现这个实例添加进去详细的HTTP context.
注意:
AddHttpSessionStateToContext方法被一个session-state module用来在当前请求中添加session 数据,在开始请求触发AcquireRequestState事件期间.在当前请求Session数据既会找回现有session或创建一个新的session.这个Session数据是封装在一个IHttpSesssionState实现的实例中.与当前的HttpContext一起传给AddHttpSessionStateToContext方法中.提供Session数据之后通过当前context的Session属性产生可是使用的应用程序代码.
(5)SessionStateUtility.RemoveHttpSessionStateFromContext方法
从context中移出session data.
参数:
cotext:就是HttpContext;
注意:
RemoveHttpSessionStateFromContext 方法是从HttpContext中清出session数据.一个 session-state module将在ReleaseRequestState事件handler中调用RemoveHttpSessionStateFromContext.
(6)SessionStateUtility.GetSessionStaticObjects Method
从context中得到一个static Object collection引用.
参数:
context:类型为HttpContext.
GetSessionStaticObjects被使用来找回定义在Global.asax文件中的static object collection.一个session-state module实现将提供返回HttpStaticObjcetCollection 集合去在IHttpSessionState实现的实例中使用AddHttpSessionStateToContext方法添加进当前context.
(7)HttpSessionStateContainer 类
包括Session-state值也有为当前去请求session-level设置.
Session data 传递和从当前的HttpContext中找回,就如HttpSessionStateContainer对象或任何有效的IHttpSessionState接口的实现都可以做到.
ASP.NET提供session-state管理允许你存储信息结合一个唯一浏览器session跨越多个请求.你能存储在用key,index控制的一个值的collection中.访问Session值并且结合通过当前请求的Sesion属性或Page的Session属性给HttpSessionState类所使用,而这个HttpSessionState类访问Sesion-state值和session-level设置都在一个Sesion-state Container设置和引用,而这个HttpSessionState interface的实现已经迁移session-state数据并且由session-state module为HttpApplication添加进当前请求的HttpContext中.
而这个HttpSessionState类调用HttpSessionStateContainer类,来管理在内存中的session的设置和值.
在HttpSessionStateContainer类是HttpSessionState interface的ASP.NET实现.这个HttpSessionStateContainer类不需要调用应用程序代码.如果你要使用custom Session-state module替换SessionStateModule,你能使用HttpSessionStateContainer class或提供你自己的IHttpSessionState interface的实现.
当创建一个SessionStateStoreData时对象一个sessionStateStoreProviderBase还能使用GetSessionStaticObjcets方法.
下面代码为一custom session-state module实现使用Hashtable存储session信息.这个module使用SessionStateUtility 类去引用当前请求的HttpContext和SessionIDManager,重新找回当前的HttpStaticObjcetCollection,并且调用Session_OnEnd事件定义在Global.asax文件.但这里没有防止多个Web请求使用相同的session 标记.
还有下面的 configuration
<configuration>
<system.web>
<httpModules>
<remove name="Session" />
<add name="Session"
type="Samples.AspNet.SessionState.MySessionStateModule" />
</httpModules>
</system.web>
</configuration>