ISessionIDManager.SaveSessionID(HttpContext, String, Boolean, Boolean) 方法

定义

将新创建的会话标识符保存到 HTTP 响应。Saves a newly created session identifier to the HTTP response.

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 请求(例如,HttpContextRequest 属性)的服务器对象的当前 Response 对象。The current HttpContext object that references server objects used to process HTTP requests (for example, the Request and Response properties).

id
String

会话标识符。The session identifier.

redirected
Boolean

该方法返回时,如果响应重定向到当前 URL(会话标识符已添加至该 URL),则包含布尔值 true;否则为 falseWhen this method returns, contains a Boolean value that is true if the response is redirected to the current URL with the session identifier added to the URL; otherwise, false.

cookieAdded
Boolean

该方法返回时,如果 Cookie 已添加至 HTTP 响应,则包含布尔值 true;否则为 falseWhen this method returns, contains a Boolean value that is true if a cookie has been added to the HTTP response; otherwise, false.

示例

下面的代码示例演示一个部分实现的 SaveSessionID 方法。The following code example shows a partially implemented SaveSessionID method. 如果自定义会话 ID 管理器支持无 cookie 会话标识符,你将需要实现用于在 URL 中发送和检索会话标识符的解决方案,如 ISAPI 筛选器。If your custom session-ID manager supports cookieless session identifiers, you will need to implement a solution for sending and retrieving session identifiers in the URL, such as an ISAPI filter.

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.AcquireRequestStateThe SaveSessionID method is called by the SessionStateModule object during the HttpApplication.AcquireRequestState event. SaveSessionID当使用无 cookie 会话状态时,该方法将在 URL (中存储会话标识符) 或在未过期的会话 cookie 中。The SaveSessionID method stores the session identifier either in the URL (when cookieless session state is used) or in a non-expiring session cookie.

如果从实现返回的值可能 CreateSessionID 包含在 HTTP 响应或请求中无效的字符,则应使用 UrlEncode 方法对方法实现中的会话标识符值进行编码 SaveSessionID ,并使用 UrlDecode 方法对方法实现中的会话标识符值进行解码 GetSessionIDIf it is possible that the value returned from your CreateSessionID implementation contains characters that are not valid in an HTTP response or request, you should use the UrlEncode method to encode the session-identifier value in your SaveSessionID method implementation and the UrlDecode method to decode the session-identifier value in your GetSessionID method implementation.

适用于