views:

56

answers:

3

I need to transfer a user to a page which requires session data which is stored within a different session.

Is it possible in some cases to migrate the session data by setting the session cookie on the user's browser?

Currently I have no workaround, and using session data seems like the only option I have at the moment.

A: 

No, browsers don't share cookies. Although if you use cookieless sessions it wiil work.

Yuriy Faktorovich
@Yuriy: I meant by changing the session id within the cookie
RadiantHex
A: 

If you have a login mechanism, you can just associate the data with the logged-in user (in a database?) instead of putting it in the session.

BalusC
+1  A: 

Is it possible in some cases to migrate the session data by setting the session cookie on the user's browser?

Sure you can. Simply set a new cookie and redirect. You will have to do this server-side. Setting a session cookie client-side will be problematic.

const string sessionStateCookieName = "ASP.NET_SessionId";
string sessionId = "some session id";

HttpCookie cookie = new HttpCookie(sessionStateCookieName, sessionId) { Path = "/", HttpOnly = true };
Response.Cookies.Remove(sessionStateCookieName);
Response.Cookies.Add(cookie);

// force a redirect to complete session switch
Response.Redirect(Request.Path);

You can expand on this. Here is a working example .aspx page. To switch sessions, simply add a url param 'sessionId'.

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
    // this is the method that does the magic
    protected override void OnPreInit(EventArgs e)
    {
        var sessionId = Request["sessionId"];

        if (!string.IsNullOrEmpty(sessionId))
        {

            // this could be found via reflection if different sessionstate cookie name is used in config
            const string sessionStateCookieName = "ASP.NET_SessionId";

            HttpCookie cookie = new HttpCookie(sessionStateCookieName, sessionId) { Path = "/", HttpOnly = true };
            Response.Cookies.Remove(sessionStateCookieName);
            Response.Cookies.Add(cookie);

            // force a redirect to complete session switch
            Response.Redirect(Request.Path);
        }

    }

    // the rest of this script is just demo code 

    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentSessionIdLabel.Text = Session.SessionID;
    }

    protected void SwitchSessionButton_Click(object sender, EventArgs e)
    {
        // this is the session we wish to switch to 
        string switchToSessionId = SessionIdTextBox.Text;
        Response.Redirect(Request.Path + "?sessionId=" + Server.UrlEncode(switchToSessionId));
    }

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
    <div>
        Current SessionId<br />
        <asp:Label ID="CurrentSessionIdLabel" runat="server"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Desired SessionId"></asp:Label>
        <br />
        <asp:TextBox ID="SessionIdTextBox" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="SwitchSessionButton" runat="server" Text="Switch Session" OnClick="SwitchSessionButton_Click" />
    </div>
    </form>
</body>
</html>
Sky Sanders