ISessionIDManager.SaveSessionID Method (HttpContext, String, Boolean, Boolean)

 

Saves a newly created session identifier to the HTTP response.

Namespace:   System.Web.SessionState
Assembly:  System.Web (in System.Web.dll)

void SaveSessionID(
	HttpContext context,
	string id,
	out bool redirected,
	out bool cookieAdded
)

Parameters

context
Type: System.Web.HttpContext

The current HttpContext object that references server objects used to process HTTP requests (for example, the Request and Response properties).

id
Type: System.String

The session identifier.

redirected
Type: System.Boolean

When 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
Type: System.Boolean

When this method returns, contains a Boolean value that is true if a cookie has been added to the HTTP response; otherwise, false.

The SaveSessionID method is called by the SessionStateModule object during the HttpApplication.AcquireRequestState event. The SaveSessionID method stores the session identifier either in the URL (when cookieless session state is used) or in a non-expiring session cookie.

If 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.

The following code example shows a partially implemented GetSessionID method. 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;
  }
}

.NET Framework
Available since 2.0
Return to top
Show: