locked
After set session null comes active RRS feed

  • Question

  • User250406165 posted

    Hi, 

    i have a login page where, i set the session data from a database

    Session["utilizador"] = dt.Rows[0][2].ToString();
    Session["user"] = dt.Rows[0][1].ToString();
    Server.Transfer("clientes.aspx");

    and a button for logout in a masterpage where i set

    Session["utilizador"] = null;
    Server.Transfer("index.aspx");

    this works perfect, redirects to login page, but if i press go back button, the page is accessible with the last session that i previous set null.

    I test this without master being page and works.

    Regards

    Saturday, May 16, 2020 12:03 AM

Answers

  • User-719153870 posted

    Hi amoniz,

    amoniz

    but if i press go back button, the page is accessible with the last session that i previous set null.

    Sorry to tell that this issue cannot be reproduced on my side.

    Below is the demo i built to test the process:

    FromPage.aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>

    FromPage.aspx.cs:

            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["utilizador"] = TextBox1.Text;
                Response.Redirect("ChildToPage.aspx");
                //Server.Transfer("ChildToPage.aspx");
            }

    ChildToPage.aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ChildToPage.aspx.cs" Inherits="WebFormDemo02.ChildToPage" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
    </asp:Content>

    ChildToPage.aspx.cs:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["utilizador"] != null)
                {
                    Label1.Text = Session["utilizador"].ToString();
                }
                else
                {
                    Label1.Text = "NoValue";
                }
            }

    Site.Master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebFormDemo02.SiteMaster" %>
    
    <!DOCTYPE html>
    
    <html lang="en">
    <head runat="server">
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title><%: Page.Title %> - My ASP.NET Application</title>
    
        <asp:PlaceHolder runat="server">
            <%: Scripts.Render("~/bundles/modernizr") %>
        </asp:PlaceHolder>
    
        <webopt:bundlereference runat="server" path="~/Content/css" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    
    </head>
    <body>
        <form runat="server">
            <asp:ScriptManager runat="server">
                <Scripts>
                    <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                    <%--Framework Scripts--%>
                    <asp:ScriptReference Name="MsAjaxBundle" />
                    <asp:ScriptReference Name="jquery" />
                    <asp:ScriptReference Name="bootstrap" />
                    <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                    <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                    <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                    <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                    <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                    <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                    <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                    <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                    <asp:ScriptReference Name="WebFormsBundle" />
                    <%--Site Scripts--%>
                </Scripts>
            </asp:ScriptManager>
    
            <div class="navbar navbar-inverse navbar-fixed-top">
                <div class="container">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" runat="server" href="~/">Application name</a>
                    </div>
                    <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li><a runat="server" href="~/">Home</a></li>
                            <li><a runat="server" href="~/About">About</a></li>
                            <li><a runat="server" href="~/Contact">Contact</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="container body-content">
                <asp:Button ID="Button1" runat="server" Text="OUT" OnClick="Button1_Click" />
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
                <hr />
                <footer>
                    <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
                </footer>
            </div>
    
        </form>
    </body>
    </html>

    Site.Master.cs:

            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["utilizador"] = null;
                Response.Redirect("FromPage.aspx");
                //Server.Transfer("FromPage.aspx");
            }

    Below is the result of this demo:

    As you can see, the session is cleared to null.

    Can you provide a complete sample that can reproduce this issue so that we can help fix it?

    In addition, you can also try Session.Clear() or Session.Abandon() rather than set it to null, please refer to this thread.

    PS. I changed Server.Transfer to Response.Redirect since Server.Transfer won't change the address bar which means when you click the button, it posts back and load current page again, this causes you need to click the logout button twice to transfer to next page. And, use the Server.Transfer cannot reproduce this issue too.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2020 5:55 AM

All replies

  • User-719153870 posted

    Hi amoniz,

    amoniz

    but if i press go back button, the page is accessible with the last session that i previous set null.

    Sorry to tell that this issue cannot be reproduced on my side.

    Below is the demo i built to test the process:

    FromPage.aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="GO" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>

    FromPage.aspx.cs:

            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["utilizador"] = TextBox1.Text;
                Response.Redirect("ChildToPage.aspx");
                //Server.Transfer("ChildToPage.aspx");
            }

    ChildToPage.aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ChildToPage.aspx.cs" Inherits="WebFormDemo02.ChildToPage" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
    </asp:Content>

    ChildToPage.aspx.cs:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["utilizador"] != null)
                {
                    Label1.Text = Session["utilizador"].ToString();
                }
                else
                {
                    Label1.Text = "NoValue";
                }
            }

    Site.Master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebFormDemo02.SiteMaster" %>
    
    <!DOCTYPE html>
    
    <html lang="en">
    <head runat="server">
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title><%: Page.Title %> - My ASP.NET Application</title>
    
        <asp:PlaceHolder runat="server">
            <%: Scripts.Render("~/bundles/modernizr") %>
        </asp:PlaceHolder>
    
        <webopt:bundlereference runat="server" path="~/Content/css" />
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    
    </head>
    <body>
        <form runat="server">
            <asp:ScriptManager runat="server">
                <Scripts>
                    <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                    <%--Framework Scripts--%>
                    <asp:ScriptReference Name="MsAjaxBundle" />
                    <asp:ScriptReference Name="jquery" />
                    <asp:ScriptReference Name="bootstrap" />
                    <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                    <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                    <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                    <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                    <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                    <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                    <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                    <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                    <asp:ScriptReference Name="WebFormsBundle" />
                    <%--Site Scripts--%>
                </Scripts>
            </asp:ScriptManager>
    
            <div class="navbar navbar-inverse navbar-fixed-top">
                <div class="container">
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" runat="server" href="~/">Application name</a>
                    </div>
                    <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li><a runat="server" href="~/">Home</a></li>
                            <li><a runat="server" href="~/About">About</a></li>
                            <li><a runat="server" href="~/Contact">Contact</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="container body-content">
                <asp:Button ID="Button1" runat="server" Text="OUT" OnClick="Button1_Click" />
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
                <hr />
                <footer>
                    <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
                </footer>
            </div>
    
        </form>
    </body>
    </html>

    Site.Master.cs:

            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["utilizador"] = null;
                Response.Redirect("FromPage.aspx");
                //Server.Transfer("FromPage.aspx");
            }

    Below is the result of this demo:

    As you can see, the session is cleared to null.

    Can you provide a complete sample that can reproduce this issue so that we can help fix it?

    In addition, you can also try Session.Clear() or Session.Abandon() rather than set it to null, please refer to this thread.

    PS. I changed Server.Transfer to Response.Redirect since Server.Transfer won't change the address bar which means when you click the button, it posts back and load current page again, this causes you need to click the logout button twice to transfer to next page. And, use the Server.Transfer cannot reproduce this issue too.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2020 5:55 AM
  • User250406165 posted

    Hi, Yang Shen

    I test your solucion and it's perfect. The problem was resolved by change as you sugered:

    Server.Transfer("index.aspx"); 

    to

    Response.Redirect("index.aspx");

    Soo, I must say to you a BIG THANK YOU for the help and your time.

    Regards,

    Thursday, May 21, 2020 10:07 PM