首页新闻找找看学习计划

封装session后,调用时后session为空?有彩蛋 0.0

0
悬赏园豆:20 [待解决问题]

祝各位大哥早日找到波多野结衣一样活好的女朋友

我在登陆后给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·
}
复制代码

 

 

少时不知贵的主页 少时不知贵 | 初学一级 | 园豆:86
提问于:2016-08-24 09:43
< >
分享
所有回答(6)
0

cookie不是要appendcookie么?忘了。。。

顾晓北 | 园豆:9520 (大侠五级) | 2016-08-24 09:50
0

baseOpen.UserInfoSessioin = obj.First();

确定这里赋值了?

 

再不行写个demo吧!

农码一生 | 园豆:118 (初学一级) | 2016-08-24 10:13
0

在一般处理程序中使用Session时,需实现”IRequiresSessionState“标记接口,指定目标 HTTP 处理程序需要对会话状态值具有读写访问权

IT火 | 园豆:198 (初学一级) | 2016-08-24 11:36
0

controller每次都会实例化,你确定每次都有赋值?

幻天芒 | 园豆:36522 (高人七级) | 2016-08-24 11:56
0

断点一步一步跟踪,看看你赋值之后有木有,生成的cookie存不存在

雨之秋水 | 园豆:640 (小虾三级) | 2016-08-24 16:01
0

此原因是:

//问题在这,如果写在上面初始化得到的数据为空        

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;
            }
        }

虽然不知道 原理

少时不知贵 | 园豆:86 (初学一级) | 2016-08-25 09:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册