单击登录时会话变量丢失

时间:2021-03-18 03:49:04

In first visit, Page_Load is running and session store correctly. When I click on Sign In button, Page_Load is call again, and then call btnSignIn_Click function, but Session is empty!

在第一次访问时,Page_Load正在运行并且会话存储正确。当我点击登录按钮时,再次调用Page_Load,然后调用btnSignIn_Click函数,但Session为空!

public partial class LoginPage : System.Web.UI.Page
{
    UserItem userItem = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Initialize and validate post
         RequestObj post = new RequestObj(Context);            
         if (post.isValid)
         {
            Session["post"] = post;
         }
    }
    protected void btnSignIn_Click(object sender, EventArgs e)
    {
        if (Session["post"] != null)
        {
            RequestObj post = Session["post"] as RequestObj;    
            userItem = Functions.LogIn(post);
        }
        Response.Redirect("LogIn.aspx");
     }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" LoginPage.aspx.cs"
         Inherits="myNameSpace.LoginPage " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=320"/>
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <div style="width:100%; text-align:center;">    
    <%if (userItem == null)
      {%>
           Username:
        <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
        <br />
        Password:
        <asp:TextBox ID="txtPass" runat="server" TextMode="Password">
        </asp:TextBox>
        <br />
        <asp:Button ID="btnSignIn" runat="server" Text="Sign In" 
            onclick="btnSignIn_Click" />
      <%}else{%>
       <%=userItem.LoginName%>
       <br />
       <%=userItem.LoginTime.ToString()%>
       <br />
        <asp:Button ID="btnSignOut" runat="server" Text="Sign Out" 
            onclick="btnSignOut_Click" />
       <%}%>
    </div>
    </form>
</body>
</html>

1 个解决方案

#1


1  

Did you mean to only set session when the page renderes initially?

你的意思是只在最初渲染页面时设置会话吗?

Try wrapping the assignment like this

尝试像这样包装任务

if(!IsPostBack)
{
   RequestObj post = new RequestObj(Context);            
   if (post.isValid)
   {
     Session["post"] = post;
   }
}

This will prevent this from being reset during a postback

这将防止在回发期间重置此项

Otherwise I would check the value of post in a debugger

否则我会在调试器中检查post的值

#1


1  

Did you mean to only set session when the page renderes initially?

你的意思是只在最初渲染页面时设置会话吗?

Try wrapping the assignment like this

尝试像这样包装任务

if(!IsPostBack)
{
   RequestObj post = new RequestObj(Context);            
   if (post.isValid)
   {
     Session["post"] = post;
   }
}

This will prevent this from being reset during a postback

这将防止在回发期间重置此项

Otherwise I would check the value of post in a debugger

否则我会在调试器中检查post的值