共用方式為


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 要求 (例如,RequestResponse 屬性) 的伺服器物件。

傳回

與 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

備註

GetSessionIDHttpApplication.EndRequest 事件期間 HttpApplication.AcquireRequestState ,會呼叫 SessionStateModule 方法。 如果您無法從 HTTP 要求擷取有效的會話識別碼,請傳回 nullSessionStateModule如果 從 GetSessionID 方法接收 null ,它會呼叫 CreateSessionID 方法,以取得新會話的新會話識別碼。

如果實 CreateSessionID 作所傳回的值可能包含在 HTTP 回應或要求中不正確字元,您應該使用 UrlEncode 方法來編碼方法實作中的會話識別碼值,以及 UrlDecode 將方法實作中的 SaveSessionIDGetSessionID 會話識別碼值解碼的方法。

適用於

另請參閱