1
votes

Session question in asp.net/c#

I have multiple asp.net pages and a class object. e.g. Class User object contains

    public string Name { set; get; }
    public string Address { set; get; }
    etc.....

pg1.aspx: when button is clicked, the data is stored in the session and redirects to pg2.aspx

e.g. pg1.aspx

    User u = new User();
    u.Name = TextBox1.Text;
    Session.Add("USERINFO", u);
    Response.Redirect("pg2.aspx");

e.g. pg2.aspx //reading the data..

    User u = ((User)Session["USERINFO"]);
    Response.Write(u.Name + "<br/>");

..etc.. ( I have 5 pages and the data is passed around from page to page in a session) once the user reaches the last page it stores in the database otherwise I dont want to store incomplete data in the database.

It all works fine except if I use multiple tabs to run same web app, the data overwrites eachother... It works fine if I use separate browsers, but not with tabs...

How can I avoid a user to enter the data in tab1 get to page 3 and if the user opens tab2 and continues not to over the data was entered in tab1....

I hope it makes sense... what I am trying to accomplish... Thank you.

2
Try and make your question summary more descriptive than Session ASP.net - Jon P
So what you are saying is on the redirect if the user has things configured to open new windows in a tab then your application doesn't work? - Wayne In Yak

2 Answers

2
votes

This is probably a duplicate question, so I'll simply redirect you to some answers that would solve your problem.

Unfortunately, the accepted answer won't work since it maintains a "unique" session identifier for each page, not your group of 5 pages.

Your browser version and the different methods of launching new tabs and windows in it determines whether sessions are shared across multiple tabs and windows. Controlling it could be done with

  • The modification to web.config as described in the linked article
  • Coming up with a unique session key prefix for each session value
  • Using some logic to prevent the same user from opening multiple sessions on the same machine
0
votes

The simplest way to work around your current implementation would likely be to check whether or not you have the information you would expect from that page, and if the conditions aren't met then require the data to be input, or redirect appropriately depending on which stage in the process you are (based on the data that is available).

So, as well as adding to the session, you can get:

var value = Session[key] as string;
if (value != null)
{
    //move on to the next appropriate page 
    //after determining which that is by other conditions
}
//we obviously need the page at which we get this data!

And you can build upon your conditions from there