ISessionIDManager 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義自訂工作階段狀態識別項管理員必須實作的合約。
public interface class ISessionIDManager
public interface ISessionIDManager
type ISessionIDManager = interface
Public Interface ISessionIDManager
- 衍生
範例
下列程式代碼範例示範實作 Cookie 型會話標識碼管理員的類別。
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 要求擷取會話標識碼值。 您可以使用 sessionState 元素的 屬性來啟用自定義會話識別碼管理員sessionIDManagerType
, (ASP.NET 設定架構) 組態專案。
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) |
確認提供的工作階段識別項是有效的。 |