locked
Strange situation (to me). Session state seems to be shared between multiple browsers/locations. RRS feed

  • Question

  • User-755601772 posted

    Ok, I think some people are after this functionality, but personally I am not in this case :)

    Built a VS2010 C# Web Project, Have a custom class for storing logged on user information ("User2"), with the [Serializable()] declaration.  I then on a web form create a new object for User2 and then try to populate everything.  After the object is created and populated, i put it in Session["ThisUser"] which i then try use on a page load of any response.redirect("..") to 'continue' with the same user.  like:

     public partial class ExpenseForm : System.Web.UI.Page
         {
              User2 ThisUser = new User2();
    
              protected void Page_Load(object sender, EventArgs e)
              {
                   if (!(Page.IsPostBack))
                   {
                        if (Session["ThisUser"] != null)
                        {
                             ThisUser = (User2)Session["ThisUser"];
    
                             if (!ThisUser.LOGINSECURED)
                             {
                                  ThisUser.RETURNURL = HttpContext.Current.Request.Url.AbsoluteUri;
                                  Response.Redirect("Login.aspx");
                             }

    On two seperate (ie/firefox/chrome browsers), after loggin in properly, if i navigate to a different page it seems that the last logon will overwrite all session data in the first session/browsers, so on the first browser if i click somewhere else it will be with the second browser's user information (name/etc).  The two browsers seems to show different Session.SessionID values however.

    This even happens if i log in on one computer and travel 50 miles to a different office.  I've tried it with InProc and StateServer.  

    At first i thought it was because i mistakenly put all the user variables inside the Global.asax, so that's why i created the seperate class to store the user data.  but now Global.asax has no reference to any of the user2 variables nor does it create any instance of the object.

    its driving me nuts. 

    What am I pulling a Simpsons on?

    Thursday, August 25, 2011 7:44 PM

Answers

  • User465171450 posted

    Are you defining all the fields as static? private static string _fp_first = string.Empty is not variable local to the object. If you are trying to access this, I would expect it to be the exact same for all users. The static keyword means it is not local to the object and the variable is actually available to all the versions of the class out there. if you update it for one user, all users immediately see the same value, until someone else changes it, then they all see that changed version. If you want a typical property that gets/sets a variable internal to the class just remove that static definition.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 26, 2011 4:28 PM

All replies

  • User-1179452826 posted

    Sessions are not shared across browsers. Most browsers let sessions be shared across tabs and possibly in different windows of the same browser. But different browsers will not share Session information. Just how the web works. If you want something different, you'll need to write your own (perhaps with the help of your database).

    Thursday, August 25, 2011 11:30 PM
  • User-755601772 posted

    I understand, that's what i'm saying, this behavior is what i'm SEEING, its not what i want.

    We're thinking it has something to do with the user2.cs object being serializable() and public?  took out the serializable() part and changed the session mode to 'inproc' but still the same thing.

    Friday, August 26, 2011 12:51 PM
  • User-755601772 posted

    ...now i'm starting to think its more likely that my user2.cs class isn't setup properly to serve up an empty instance of itself whenever its created...  am i missing something in thinking that saying "User2 ThisUser = new User2();" will create a new EMPTY instance of the User2 object for each session?

    namespace Business4
     {
     //    [Serializable()]
         public class User2
         {
              private static string _fp_first = string.Empty;
              public string FP_FIRST
              {
                   get { return _fp_first; }
                   set { _fp_first = value; }
              }
    .....}
    
         public String GetSignatureSelectorsData()
              {
                   String processed = "";
    
                   using (SqlConnection conn = new SqlConnection(Global.connString))
                   {
                        conn.Open();
    
                        using (SqlCommand cmd = new SqlCommand("GetSignatureSelectors", conn))
                        {
                             cmd.CommandType = CommandType.StoredProcedure;
    
                             try
                             {
                                  SqlDataReader dr = cmd.ExecuteReader();
    
                                  if (dr.HasRows)
                                  {....
    Friday, August 26, 2011 1:42 PM
  • User-1179452826 posted

    I couldn't understand your code, but here's the deal:

    1. You won't be able to share sessions across browsers.

    2. What you can do is fetch the db entry on every request. That way, a change in one browser will be reflected in another.

    Friday, August 26, 2011 1:54 PM
  • User465171450 posted

    Are you defining all the fields as static? private static string _fp_first = string.Empty is not variable local to the object. If you are trying to access this, I would expect it to be the exact same for all users. The static keyword means it is not local to the object and the variable is actually available to all the versions of the class out there. if you update it for one user, all users immediately see the same value, until someone else changes it, then they all see that changed version. If you want a typical property that gets/sets a variable internal to the class just remove that static definition.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 26, 2011 4:28 PM
  • User-755601772 posted

    If i had a hundred bucks i'd give it to you, :) THANK YOU!

    Friday, August 26, 2011 4:45 PM
  • User465171450 posted

    Not a problem. It's often the simplest things, even one keyword, that provides the biggest headaches.

    Friday, August 26, 2011 4:47 PM
  • User-755601772 posted

    we were just saying that this morning, it has to be something stupid/obvious that we're missing.  but at least i learned something and might have a use for the mechanism the way i had it 'broken'.

    thank again.

    Friday, August 26, 2011 5:35 PM