Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SessionID keeps changing in ASP.NET MVC why?

I am trying to keep track of something and using the SessionID as they key to that object

However the SessionID every 2-3 reqiests changes shouldn't it remain the same?

HttpContext.Session.SessionID 

Is the code I am using.

like image 731
dswatik Avatar asked Nov 11 '08 19:11

dswatik


People also ask

What causes session ID to change?

Every time an Internet user visits a specific Web site, a new session ID is assigned. Closing a browser and then reopening and visiting the site again generates a new session ID.

Can session ID be duplicated?

Yes, Session. SessionId can be duplicate.

Can we maintain session in MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same.


2 Answers

I've seen that happen even without MVC. If I remember correctly, ASP.NET keeps assigning new session ids until you place something into the Session variable.

like image 90
Maxam Avatar answered Sep 21 '22 08:09

Maxam


You should initialize the Session object within Global.asax.cs.

void Session_Start(object sender, EventArgs e) {     HttpContext.Current.Session.Add("__MyAppSession", string.Empty); } 

This way the session will not change unless your browser window is closed.

like image 24
javierlinked Avatar answered Sep 21 '22 08:09

javierlinked