祝各位大哥早日找到波多野结衣一样活好的女朋友
我在登陆后给session赋值后,跟断点到UserInfoSessioin函数时候,看到函数里面HttpContext.Current.Session,cookie都是空的。
按理说只要给session赋值,就可以全局使用,不可能为空啊!
后台调用:
namespace food.web { public partial class index : System.Web.UI.Page {
//////////////////////////////调用//////////////////////////// public user admin = baseOpen.UserInfoSessioin; protected void Page_Load(object sender, EventArgs e) { if (admin == null) Response.End(); string type = Request["type"]; switch (type) { case "tree": Response.Write( LvComm.getTree()); Response.End(); break; } } } }
登陆界面:登陆成功给session、cookie赋值
model = baseOpen.GetFormToM(new user { }, Request); var obj = comm.GetList(x => x.Card == model.Card && x.Pwd == model.Pwd); if (obj.Count() > 0) { ///////////////只要看这里给session赋值//////////////////// baseOpen.UserInfoSessioin = obj.First(); HttpCookie Cookie = Response.Cookies["userInfo"]; //cookie记录 if (Cookie == null) { Cookie = new HttpCookie("userInfo"); Cookie.Values.Add("Card", model.Card); Cookie.Values.Add("Pwd", model.Pwd); //设置Cookie过期时间 Cookie.Expires = DateTime.Now.AddDays(365); Response.Cookies.Add(Cookie); } else { Cookie.Values["Card"] = model.Card; Cookie.Values["Pwd"] = model.Pwd; Response.SetCookie(Cookie); } Response.Redirect("/index.aspx"); }
方法:
public class baseOpen { private static MyContext db = new MyContext(); #region 01 户信息Session /// <summary> /// 用户信息Session /// </summary> public static user UserInfoSessioin { get { if (HttpContext.Current.Session != null && HttpContext.Current.Session["userinfo"] != null) { return HttpContext.Current.Session["userinfo"] as user; } else { HttpCookie Cookie = HttpContext.Current.Response.Cookies["userInfo"]; if (Cookie != null) { var lodname = Cookie.Values["Card"]; var lodpass = Cookie.Values["Pwd"]; if (!string.IsNullOrEmpty(lodname) && !string.IsNullOrEmpty(lodpass)) { var objuser = db.users.Where(x => x.Card == lodname && x.Pwd == lodpass).FirstOrDefault(); if (null != objuser) { HttpContext.Current.Session["userinfo"] = objuser; return objuser; } } } return null; } } set { if (HttpContext.Current.Session != null) HttpContext.Current.Session["userinfo"] = value; } } #endregion· }
cookie不是要appendcookie么?忘了。。。
baseOpen.UserInfoSessioin = obj.First();
确定这里赋值了?
再不行写个demo吧!
在一般处理程序中使用Session时,需实现”IRequiresSessionState“标记接口,指定目标 HTTP 处理程序需要对会话状态值具有读写访问权
controller每次都会实例化,你确定每次都有赋值?
断点一步一步跟踪,看看你赋值之后有木有,生成的cookie存不存在
此原因是:
//问题在这,如果写在上面初始化得到的数据为空
public user admin = baseOpen.UserInfoSessioin; protected void Page_Load(object sender, EventArgs e) { if (admin == null) Response.End(); string type = Request["type"]; switch (type) { case "tree": Response.Write( LvComm.getTree()); Response.End(); break; } }
解决方法:
public user admin ; protected void Page_Load(object sender, EventArgs e) { //在里面赋值 admin = baseOpen.UserInfoSessioi if (admin == null) Response.End(); string type = Request["type"]; switch (type) { case "tree": Response.Write( LvComm.getTree()); Response.End(); break; } }
虽然不知道 原理