自定义SessionIDManager
(1)SessionIDManager Class
为ASP.NET session state的管理唯一标识.
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class SessionIDManager : ISessionIDManager
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class SessionIDManager : ISessionIDManager
SessionIDManager类是一个为ASP.NET session state管理唯一Session标识HTTP module.这个ASP.NET session 唯一标识是SessionID属性,这个属性是当前HttpContext或Page的Session属性.这个ASP.NET session 标识是随机产生24字符串,这24字符串由a-z的小写符和0-5的数字.
默认,这个SessionID值被发送每次请求ASP.NET应用程序时的cookie中.在cookie中的名为ASP.NET_SessionId中包括SessionID值并且是默认的.你能通过配置sessionState Element (ASP.NET Settings Schema)中的cookieName attribute设置一个在Cookie中不同名.
如果你要在你的ASP.NET应用程序不授权使用cookies并且仍然确定要使用Session state,你要在你的应用程序Web.config文件中能设置sessionState元素的cookieles属性设置为true,这样你的应用程序的Session 唯一标识就存储在URL中.
使用的属性就是:
HttpSessionStateContainer.IsCookieless Property
public bool IsCookieless { get; }
public bool IsCookieless { get; }
注意:
你能自己创建一个继承SessonIDManager类和重写CreateSessionID和自己实现一个Validate方法来替换ASP.NET session 标识.具体看http://msdn2.microsoft.com/en-us/library/ms153031.aspx
下面也有详细内容
你还能通过创建一个类去实现ISessionIDManager interface自己去管理Session-ID manager取代SessionIDManager.如你的custom session-ID manager要支持cookies session 唯一标识,你将需要实现一个发送和找回在URL中session唯一标识.如果ISAPI filter,具体可以看
http://msdn2.microsoft.com/en-us/library/ms153008.aspx
下面也有详细内容
CreateSessionID方法
其实这个类 主要的就是CreateSessionID方法:现在来看一下:
为Session创建一个唯一标识.
public virtual string CreateSessionID(
HttpContext context
)
HttpContext context
)
参数是HttpContext类.
返回值是为一个唯一的session标记.
CreateSessionID method是被用来产生一个唯一session标识,这个唯一session标识被SessionStateModule对象识别一个新的session.CreateSessionID方法返回值就是由CreateSessionID必须是唯一的并且必须有验证存储有HTTP响应和请求的字符.如果可能CreateSessionID实现包括字符串但没验证HTTP响应和请求,你应该在你的SavaSessionID中使用URLEncode方法去encode这个session-identifer值并且在你的GetSessionID方法中使用UrlDecode方法decode你的session-identifier.
你能自定义session 标识给ASP.NET session state使用,这个继承自定义session 标识就是SessionIDManager类和重写CreateSessionID和自定义实现Validate方法.如果你自定义sessionID时默认的Validate的实现不能适合你所是使用的字符约束,你应该重写Validate方法.
下面是显示一个继承SessionIDManager类和重写CreateSessionID和Validate方法.验证的Guid SessionID.
using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;
namespace Samples.AspNet.Session
{
public class GuidSessionIDManager : SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
}
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;
namespace Samples.AspNet.Session
{
public class GuidSessionIDManager : SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
}
配置文件:
<sessionState
Mode="InProc"
stateConnectionString="tcp=127.0.0.1:42424"
stateNetworkTimeout="10"
sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
sqlCommandTimeout="30"
customProvider=""
cookieless="false"
regenerateExpiredSessionId="false"
timeout="20"
sessionIDManagerType="Your.ID.Manager.Type,
CustomAssemblyNameInBinFolder"
/>
看在Validate介绍的实现:
using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;
namespace Samples.AspNet.Session
{
public class GuidSessionIDManager : SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
}
replace the SessionID HTTP module in your Web.config file with your custom class, as shown in the following example.(就是不使用cookies)
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;
namespace Samples.AspNet.Session
{
public class GuidSessionIDManager : SessionIDManager
{
public override string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
public override bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
}
<httpModules>
<remove name="SessionID" />
<add name="SessionID"
type="Samples.AspNet.Session.GuidSessionIDManager" />
</httpModules>
<remove name="SessionID" />
<add name="SessionID"
type="Samples.AspNet.Session.GuidSessionIDManager" />
</httpModules>
ISessionIDManager Interface
定义契约一个自定义session-state标识管理者必须实现它.
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public interface ISessionIDManager
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public interface ISessionIDManager
ISessionIDManager interface 识别你必须实现创建一个自己定义为session-identifier values manager,一个ISessionIDManager interface实现创建和验证session-identifier的values,并管理在HTTP响应中的session-identifier值的存储
,也管理从HTTP请求中找回session-identifier值.
如果你的ISessionIDManager interface实现支持cookieless session 标识,你将需要实现从URL中一个发送和找回Session唯一标识,如ISAPI filter.
你能自定义session 标识给ASP.NET session state使用,这个自定义session 标识就是继承SessionIDManager类和重写CreateSessionID和自定义实现Validate方法.这样授权你提供自己的session-identifier 值.能依赖于基于SessionIDManager类去存储HTTP 响应值和从HTTP请求找的值.
下面实现一个一个cookies-based session-ID manager的类;
using System;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;
namespace Samples.AspNet.Session
{
public class MySessionIDManager : IHttpModule, ISessionIDManager
{
private SessionStateSection pConfig = null;
//
// IHttpModule Members
//
//
// IHttpModule.Init
//
public void Init(HttpApplication app)
{
// Obtain session-state configuration settings.
if (pConfig == null)
{
Configuration cfg =
WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
pConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");
}
}
//
// IHttpModule.Dispose
//
public void Dispose()
{
}
//
// ISessionIDManager Members
//
//
// ISessionIDManager.Initialize
//
public void Initialize()
{
}
//
// ISessionIDManager.InitializeRequest
//
public bool InitializeRequest(HttpContext context,
bool suppressAutoDetectRedirect,
out bool supportSessionIDReissue)
{
if (pConfig.Cookieless == HttpCookieMode.UseCookies)
{
supportSessionIDReissue = false;
return false;
}
else
{
supportSessionIDReissue = true;
return context.Response.IsRequestBeingRedirected;
}
}
//
// ISessionIDManager.GetSessionID
//
public string GetSessionID(HttpContext context)
{
string id = null;
if (pConfig.Cookieless == HttpCookieMode.UseUri)
{
// Retrieve the SessionID from the URI.
}
else
{
id = context.Request.Cookies[pConfig.CookieName].Value;
}
// Verify that the retrieved SessionID is valid. If not, return null.
if (!Validate(id))
id = null;
return id;
}
//
// ISessionIDManager.CreateSessionID
//
public string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
//
// ISessionIDManager.RemoveSessionID
//
public void RemoveSessionID(HttpContext context)
{
context.Response.Cookies.Remove(pConfig.CookieName);
}
//
// ISessionIDManager.SaveSessionID
//
public void SaveSessionID(HttpContext context, string id, out bool redirected, out bool cookieAdded)
{
redirected = false;
cookieAdded = false;
if (pConfig.Cookieless == HttpCookieMode.UseUri)
{
// Add the SessionID to the URI. Set the redirected variable as appropriate.
redirected = true;
return;
}
else
{
context.Response.Cookies.Add(new HttpCookie(pConfig.CookieName, id));
cookieAdded = true;
}
}
//
// ISessionIDManager.Validate
//
public bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
}
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.SessionState;
namespace Samples.AspNet.Session
{
public class MySessionIDManager : IHttpModule, ISessionIDManager
{
private SessionStateSection pConfig = null;
//
// IHttpModule Members
//
//
// IHttpModule.Init
//
public void Init(HttpApplication app)
{
// Obtain session-state configuration settings.
if (pConfig == null)
{
Configuration cfg =
WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
pConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");
}
}
//
// IHttpModule.Dispose
//
public void Dispose()
{
}
//
// ISessionIDManager Members
//
//
// ISessionIDManager.Initialize
//
public void Initialize()
{
}
//
// ISessionIDManager.InitializeRequest
//
public bool InitializeRequest(HttpContext context,
bool suppressAutoDetectRedirect,
out bool supportSessionIDReissue)
{
if (pConfig.Cookieless == HttpCookieMode.UseCookies)
{
supportSessionIDReissue = false;
return false;
}
else
{
supportSessionIDReissue = true;
return context.Response.IsRequestBeingRedirected;
}
}
//
// ISessionIDManager.GetSessionID
//
public string GetSessionID(HttpContext context)
{
string id = null;
if (pConfig.Cookieless == HttpCookieMode.UseUri)
{
// Retrieve the SessionID from the URI.
}
else
{
id = context.Request.Cookies[pConfig.CookieName].Value;
}
// Verify that the retrieved SessionID is valid. If not, return null.
if (!Validate(id))
id = null;
return id;
}
//
// ISessionIDManager.CreateSessionID
//
public string CreateSessionID(HttpContext context)
{
return Guid.NewGuid().ToString();
}
//
// ISessionIDManager.RemoveSessionID
//
public void RemoveSessionID(HttpContext context)
{
context.Response.Cookies.Remove(pConfig.CookieName);
}
//
// ISessionIDManager.SaveSessionID
//
public void SaveSessionID(HttpContext context, string id, out bool redirected, out bool cookieAdded)
{
redirected = false;
cookieAdded = false;
if (pConfig.Cookieless == HttpCookieMode.UseUri)
{
// Add the SessionID to the URI. Set the redirected variable as appropriate.
redirected = true;
return;
}
else
{
context.Response.Cookies.Add(new HttpCookie(pConfig.CookieName, id));
cookieAdded = true;
}
}
//
// ISessionIDManager.Validate
//
public bool Validate(string id)
{
try
{
Guid testGuid = new Guid(id);
if (id == testGuid.ToString())
return true;
}
catch
{
}
return false;
}
}
}
worksguo