ISessionIDManager.SaveSessionID(HttpContext, String, Boolean, Boolean) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將新建立的會話識別碼儲存在 HTTP 回應中。
public:
void SaveSessionID(System::Web::HttpContext ^ context, System::String ^ id, [Runtime::InteropServices::Out] bool % redirected, [Runtime::InteropServices::Out] bool % cookieAdded);
public void SaveSessionID(System.Web.HttpContext context, string id, out bool redirected, out bool cookieAdded);
abstract member SaveSessionID : System.Web.HttpContext * string * bool * bool -> unit
Public Sub SaveSessionID (context As HttpContext, id As String, ByRef redirected As Boolean, ByRef cookieAdded As Boolean)
參數
- context
- HttpContext
目前 HttpContext 參考用於處理 HTTP 請求的伺服器物件(例如 the Request and Response properties)。
- id
- String
會話標識碼。
- redirected
- Boolean
當此方法回傳時,包含一個布林值,即 true 回應被重新導向當前 URL 並加上會話識別碼;否則,為 false。
- cookieAdded
- Boolean
當此方法回傳時,包含一個布林值,即 true 若 HTTP 回應中加入了 cookie;否則, false。
範例
以下程式碼範例展示了一種部分實作 SaveSessionID 的方法。 如果你的自訂會話識別碼管理器支援無 Cookie 會話識別碼,你需要實作一個解決方案來在 URL 中傳送和檢索會話識別碼,例如 ISAPI 過濾器。
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;
}
}
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
備註
SaveSessionID該方法由物件在事件期間SessionStateModule呼叫HttpApplication.AcquireRequestState。 此 SaveSessionID 方法會將會話識別碼儲存在網址(使用無 cookie 會話狀態時)或非過期的會話 Cookie 中。
如果你的 CreateSessionID 實作回傳的值可能包含 HTTP 回應或請求中無效的字元,你應該在方法實作中使用 Method UrlEncode 編碼 session-identifier 值 SaveSessionID ,並在方法實作中 UrlDecode 用解碼 session-identifier 值 GetSessionID 的方法。