Search code examples
c#asp.netsessionsessionid

How to Generate a new Session ID


Is it possible to generate a new ID for the session using ASP.NET?

I want it to change when someone logs in to my website just before I set their initial session variables.


Solution

  • You can do this using the SessionIdManager class:

    SessionIDManager manager = new SessionIDManager();
    
    string newID = manager.CreateSessionID(Context);
    bool redirected = false;
    bool isAdded = false;
    manager.SaveSessionID(Context, newID, out redirected, out isAdded);
    

    [Code sample is from Anas Ghanem's article]