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
引用用于处理 HTTP 请求的服务器对象的当前 HttpContext 对象(例如 Request ,和 Response 属性)。
- id
- String
会话标识符。
- redirected
- Boolean
此方法返回时,包含一个布尔值,即 true 响应重定向到当前 URL,并将会话标识符添加到 URL;否则为 false。
- cookieAdded
- Boolean
此方法返回时,包含一个布尔值,即 true Cookie 已添加到 HTTP 响应;否则为 false。
示例
下面的代码示例演示了一个部分实现 SaveSessionID 的方法。 如果自定义会话 ID 管理器支持无 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 将会话标识符存储在 URL(使用无 Cookie 会话状态时)或未过期的会话 Cookie 中。
如果从 CreateSessionID 实现返回的值可能包含在 HTTP 响应或请求中无效的字符,则应使用 UrlEncode 该方法在方法实现中 SaveSessionID 对会话标识符值进行编码,以及 UrlDecode 解码方法实现中的 GetSessionID 会话标识符值的方法。