locked
'shared' variables are shared across users of same website! RRS feed

  • Question

  • User297437924 posted

    I am porting a desktop program to asp.net.   It uses shared variables (static in C#).    So I was worried that if you have 2 users of the same website, if one changes the variable, it will be changed for the other user as well.   I tested this, and it seems to be true.   If it isn't let me know.   I created 2 asp.net pages, expt1.aspx and expt2.aspx, both of which have a link to expt3.aspx.   Expt1.aspx sets a shared variable to '5'.  You then click on a link in expt1.aspx and then you are taken to expt3.aspx, which shows the value in a label.   expt2 does the same thing, except it sets the variable to '7'.

    So I opened 2 browsers (EDGE and chrome), opened expt1 in one, and expt2 in the second.   I clicked on the link in expt1 in EDGE, and it produced a 5 in expt3.   I clicked on the link in expt2 in Chrome and it produced a 7 in expt3.   Then I did a refresh of expt3 in EDGE - and it showed a 7!

    This is hard to believe, so I just want to double check if shared variables are shared across users.   It seems they are.   The pages I used are listed below:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="expt1.aspx.vb" codefile="expt1.aspx.vb" inherits="expt1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <a href="expt3.aspx">click to set to 5</a>
            </div>
        </form>
    </body>
    </html>
    Public Class expt1
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ClassConstantsEcho.expt = 5
        End Sub
    
    End Class
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="expt2.aspx.vb" Inherits="expt2"  codefile="expt2.aspx.vb"%>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <a href="expt3.aspx">to to expt3.aspx, this sets to 7</a>
            </div>
        </form>
    </body>
    </html>
    Public Class expt2
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ClassConstantsEcho.expt = 7
        End Sub
    
    End Class
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="expt3.aspx.vb" codefile="expt3.aspx.vb" inherits="expt3" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </form>
    </body>
    </html>
    Public Class expt3
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                Label1.Text = "value of 'echo' is " & ClassConstantsEcho.expt
            End If
        End Sub
    
    End Class

    Friday, March 6, 2020 5:29 PM

Answers

  • User-158764254 posted

    This is hard to believe, so I just want to double check if shared variables are shared across users

    Shared/static variables are absolutely shared by all the web apps users.

    If you want something user specific you could consider:

    • Cookies
    • Session
    • Input fields / Hidden input
    • Database

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2020 5:45 PM