Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating new SessionId in ASP.NET

Tags:

asp.net

On login I want to generate a new SessionId. I have found one solution that works, but it requires some pretty hackish things and requires the app have Full Trust securityPolicy setting.

Is there any other way to achieve this?

like image 864
Jarrod Everett Avatar asked Aug 27 '12 19:08

Jarrod Everett


People also ask

How do I create a new session ID?

Description ¶session_create_id() is used to create new session id for the current session. It returns collision free session id. If session is not active, collision check is omitted. Session ID is created according to php.

How is ASP NET_SessionId generated?

NET_SessionId has been created by the server and set in the Response Header. Now an ASP. NET_SessionId has been created by the web server so in the next requests, the Request Header has that ASP. NET_SessionId and it is the same as the Response Header.

What is ASP.NET session ID?

The SessionID property is used to uniquely identify a browser with session data on the server. The SessionID value is randomly generated by ASP.NET and stored in a non-expiring session cookie in the browser. The SessionID value is then sent in a cookie with each request to the ASP.NET application.

Can session ID be duplicated?

Yes, Session. SessionId can be duplicate.


1 Answers

Looks like this works:

Session.Abandon();
Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));

By clearing out that cookie, a new session with a new session ID will be created at the server.

(Reference: Microsoft Support)

EDIT: Here's an example using AJAX (with jQuery) to call the server code without a page refresh - it calls twice, once to remove the first session, and once to generate a new one. There may be a better way, but this does work.

function newSession() {
    jQuery.ajax({
        type: "POST",
        url: "WebForm1.aspx/ClearSession",
        data: "{}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function () {
            jQuery.ajax({
                type: "POST",
                url: "WebForm1.aspx/NewSession",
                data: "{}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function () { console.log("Success!"); },
                error: function (x, y, z) {
                    console.log("Failure!");
                }
            });
        },
        error: function (x, y, z) {
            console.log("Failure!");
        }
    });
}

And on the code-behind (for WebForms - you could also do this with an MVC controller):

[WebMethod]
public static void ClearSession()
{
    HttpContext.Current.Session.Abandon();
    HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
}

[WebMethod]
public static void NewSession()
{
    HttpContext.Current.Session["x"] = 123;
}
like image 107
Joe Enos Avatar answered Nov 15 '22 04:11

Joe Enos