我尝试添加会话,然后在重定向后我在另一个页面上访问,这总是显示空
Session.Add("pUser_Name", ds_1.Tables[0].Rows[0]["User_Name"].ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Redirect("DashBoard.aspx", false);
在另一个页面上我正在这样访问
protected void Page_Load(object sender, EventArgs e)
{
if (Session["pUser_Name"] == null) ////here is error NULL
{
Response.Redirect("Admin.aspx");
}
}
I tried
protected override void OnInit(EventArgs e)
{
if (Session["pUser_Name"] == null)
{
Response.Redirect("Admin.aspx");
}
base.OnInit(e);
}
并且
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DashBoard.aspx.cs" Inherits="SC.DashBoard" EnableTheming="true"
Theme="Theme1" EnableSessionState="True" %>
和管理页面,其中为会话分配值
<%@ Page Language="C#" AutoEventWireup="true" EnableSessionState="True" CodeBehind="Admin.aspx.cs" Inherits="SC.Admin" %>
和 web.config 文件
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<sessionState timeout="12000"/>
</system.web>
但仍然面临相同的会话为空
基于此answer https://stackoverflow.com/questions/2874078/asp-net-session-sessionid-changes-between-requests/2874174#2874174基本上指的是这个msdn 文章 https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx about SessionID
,您可能需要初始化您的会话对象会话开始:
protected void Session_Start(Object sender, EventArgs e)
{
Session["init"] = 0;
}
当会话状态存储在 cookie 中(您当前根据 web.config 使用该 cookie)时,这是必需的。原因是 ASP.NET 在第一次使用之前不会保存会话,因此每次都会生成一个新的 SessionIDnew当您发生页面请求时Response.Redirect
到另一页。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)