語言

ISessionIDManager.GetSessionID(HttpContext) 方法

定義

從目前 HTTP 請求的上下文中取得會話識別碼。

public:
 System::String ^ GetSessionID(System::Web::HttpContext ^ context);
public string GetSessionID(System.Web.HttpContext context);
abstract member GetSessionID : System.Web.HttpContext -> string
Public Function GetSessionID (context As HttpContext) As String

參數

context
HttpContext

目前 HttpContext 參考用於處理 HTTP 請求的伺服器物件(例如 the Request and Response properties)。

傳回

隨 HTTP 請求傳送的當前會話識別碼。

範例

以下程式碼範例展示了一種部分實作 GetSessionID 的方法。 如果你的自訂會話識別碼管理器支援無 Cookie 會話識別碼,你需要實作一個解決方案來在 URL 中傳送和檢索會話識別碼,例如 ISAPI 過濾器。

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;
}
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

備註

此GetSessionID方法由事件期間SessionStateModuleHttpApplication.AcquireRequestState與事件所稱呼HttpApplication.EndRequest。 若無法從 HTTP 請求取得有效的會話識別碼,請回傳 null。 如果 從SessionStateModule該方法接收nullGetSessionID到,它會呼叫該CreateSessionID方法以取得新會話的新會話識別碼。

如果你的實作回傳CreateSessionID的值可能包含 HTTP 回應或請求中無效的字元,你應該在方法實作中使用該方法編碼會話識別碼值UrlEncode,並在方法實作中使用SaveSessionIDUrlDecode解碼會話識別碼值GetSessionID的方法。

適用於

另請參閱