ISessionIDManager 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义自定义会话状态标识符管理器必须实现的协定。
public interface class ISessionIDManager
public interface ISessionIDManager
type ISessionIDManager = interface
Public Interface ISessionIDManager
- 派生
示例
下面的代码示例演示了实现基于 Cookie 的会话 ID 管理器的类。
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;
}
}
}
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web
Imports System.Web.SessionState
Namespace Samples.AspNet.Session
Public Class MySessionIDManager
Implements IHttpModule, ISessionIDManager
Private pConfig As SessionStateSection = Nothing
'
' IHttpModule Members
'
'
' IHttpModule.Init
'
Public Sub Init(app As HttpApplication) Implements IHttpModule.Init
' Obtain session-state configuration settings.
If pConfig Is Nothing Then
Dim cfg As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration( _
System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath)
pConfig = CType(cfg.GetSection("system.web/sessionState"), SessionStateSection)
End If
End Sub
'
' IHttpModule.Dispose
'
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
'
' ISessionIDManager.Initialize
'
Public Sub Initialize() Implements ISessionIDManager.Initialize
End Sub
'
' ISessionIDManager.InitializeRequest
'
Public Function InitializeRequest(context As HttpContext, _
suppressAutoDetectRedirect As Boolean, _
ByRef supportSessionIDReissue As Boolean) As Boolean _
Implements ISessionIDManager.InitializeRequest
If pConfig.Cookieless = HttpCookieMode.UseCookies Then
supportSessionIDReissue = False
Return False
Else
supportSessionIDReissue = True
Return context.Response.IsRequestBeingRedirected
End If
End Function
'
' ISessionIDManager Members
'
'
' ISessionIDManager.GetSessionID
'
Public Function GetSessionID(context As HttpContext) As String _
Implements ISessionIDManager.GetSessionID
Dim id As String = Nothing
If pConfig.Cookieless = HttpCookieMode.UseUri Then
' Retrieve the SessionID from the URI.
Else
id = context.Request.Cookies(pConfig.CookieName).Value
End If
' Verify that the retrieved SessionID is valid. If not, return Nothing.
If Not Validate(id) Then _
id = Nothing
Return id
End Function
'
' ISessionIDManager.CreateSessionID
'
Public Function CreateSessionID(context As HttpContext) As String _
Implements ISessionIDManager.CreateSessionID
Return Guid.NewGuid().ToString()
End Function
'
' ISessionIDManager.RemoveSessionID
'
Public Sub RemoveSessionID(context As HttpContext) _
Implements ISessionIDManager.RemoveSessionID
context.Response.Cookies.Remove(pConfig.CookieName)
End Sub
'
' ISessionIDManager.SaveSessionID
'
Public Sub SaveSessionID(context As HttpContext, _
id As String, _
ByRef redirected As Boolean, _
ByRef cookieAdded As Boolean) _
Implements ISessionIDManager.SaveSessionID
redirected = False
cookieAdded = False
If pConfig.Cookieless = HttpCookieMode.UseUri Then
' 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
End If
End Sub
'
' ISessionIDManager.Validate
'
Public Function Validate(id As String) As Boolean _
Implements ISessionIDManager.Validate
Try
Dim testGuid As Guid = New Guid(id)
If id = testGuid.ToString() Then _
Return True
Catch
End Try
Return False
End Function
End Class
End Namespace
注解
接口 ISessionIDManager 标识必须实现的方法,以便为会话标识符值创建自定义管理器。
ISessionIDManager接口实现创建并验证会话标识符值,并管理 HTTP 响应中会话标识符的存储以及从 HTTP 请求检索会话标识符值。 可以使用 sessionIDManagerType
sessionState 元素 (ASP.NET 设置架构) 配置元素 的 属性启用自定义会话 ID 管理器。
ISessionIDManager如果接口实现将支持无 Cookie 会话标识符,则需要实现用于在 URL 中发送和检索会话标识符的解决方案,例如 ISAPI 筛选器。
如果只想提供 ASP.NET 会话状态要使用的自定义会话标识符值,则可以创建一个继承 SessionIDManager 类的类,并使用自己的自定义实现仅 CreateSessionID 替代 和 Validate 方法。 这使你可以提供自己的会话标识符值,同时依赖基 SessionIDManager 类将值存储到 HTTP 响应并从 HTTP 请求中检索值。 有关重写 SessionIDManager 类和实现这些方法的示例,请参阅为 CreateSessionID 类的 SessionIDManager 方法提供的示例。
方法
CreateSessionID(HttpContext) |
创建一个唯一的会话标识符。 |
GetSessionID(HttpContext) |
从当前 HTTP 请求的上下文中获取会话标识符。 |
Initialize() |
初始化 SessionIDManager 对象。 |
InitializeRequest(HttpContext, Boolean, Boolean) |
针对每个请求执行 SessionIDManager 对象的初始化。 |
RemoveSessionID(HttpContext) |
从 Cookie 或 URL 删除会话标识符。 |
SaveSessionID(HttpContext, String, Boolean, Boolean) |
将新创建的会话标识符保存到 HTTP 响应。 |
Validate(String) |
确认提供的会话标识符有效。 |