Nina Nanu
Nina Nanu

Reputation: 31

ASP.NET WebForms - Create and use a new session in current HttpContext

I'm using cookieless sessions an ASP.NET WebForms 4.0 application.

Is it possible to create a new session during a PostBack (e.g. when my projectID parameter was changed) and store data in new session?

// Example url with session-1: http://localhost:12345/(S(session-1))/page1.aspx?projectID=2

protected void Page_Load(object sender, EventArgs e)
{
    string projectID = Request.Params["projectID"];

    // When projectID parameter was changed since last request, create a new session for e.g. "Project 2"
    if (Session["ProjectID"] != projectID)
    {
        // Create new session (e.g. session-2) and use it as of now in current HttpContext
    }

    // Save projectID in session-2 instead of in session-1
    Session["ProjectID"] = projectID;

Upvotes: 2

Views: 2465

Answers (1)

fdfey
fdfey

Reputation: 587

try to use SessionIDManager.

System.Web.SessionState.SessionIDManager manager = new System.Web.SessionState.SessionIDManager();
                    string newCaller = manager.CreateSessionID(HttpContext.Current);
                    bool isAdded = false;
                    bool isRedirect = false;
                    manager.SaveSessionID(HttpContext.Current, newCaller, out isRedirect, out isAdded);
                    SessionID = newCaller;

Upvotes: 1

Related Questions