asp.net cookie session操作

设置:

 

Session["mobile"] = mobile;
            Session["memberid"] = ds.Tables[0].Rows[0]["id"].ToString();
            Session["nickname"] = ds.Tables[0].Rows[0]["nickname"].ToString();
            Session["username"] = ds.Tables[0].Rows[0]["username"].ToString();

            //自动登录
            if (chklogin.Checked)
            {
                Response.Cookies["member"]["mobile"] = HttpUtility.UrlEncode(mobile);
                Response.Cookies["member"]["memberid"] = HttpUtility.UrlEncode(ds.Tables[0].Rows[0]["id"].ToString());
                Response.Cookies["member"]["nickname"] = HttpUtility.UrlEncode(ds.Tables[0].Rows[0]["nickname"].ToString());
                Response.Cookies["member"]["username"] = HttpUtility.UrlEncode(ds.Tables[0].Rows[0]["username"].ToString());
                Response.Cookies["member"].Expires = DateTime.Now.AddDays(1d);
            }

 需要进行HttpUtility.UrlEncode()编码,不然会出现乱码而无法取得cookie的值

读取:

if (Request.Cookies["member"] != null)
        {
            if (Request.Cookies["member"]["mobile"] != null && Request.Cookies["member"]["memberid"] != null && Request.Cookies["member"]["nickname"] != null && Request.Cookies["member"]["username"] != null)
            {
                Session["memberid"] = HttpUtility.UrlDecode(Request.Cookies["member"]["memberid"]);
                Session["nickname"] = HttpUtility.UrlDecode(Request.Cookies["member"]["nickname"]);
                Session["username"] = HttpUtility.UrlDecode(Request.Cookies["member"]["username"]);
                Session["mobile"] = HttpUtility.UrlDecode(Request.Cookies["member"]["mobile"]);
            }
        }
 需要进行HttpUtility.UrlDecode解码,不然无法取得cookie的值

检测:

public static void CheckUserLogin()
    {
        if (HttpContext.Current.Request.Cookies["member"] != null)
        {
            if (HttpContext.Current.Request.Cookies["member"]["mobile"] != null && HttpContext.Current.Request.Cookies["member"]["memberid"] != null && HttpContext.Current.Request.Cookies["member"]["nickname"] != null && HttpContext.Current.Request.Cookies["member"]["username"] != null)
            {
                HttpContext.Current.Session["memberid"] = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["member"]["memberid"]);
                HttpContext.Current.Session["nickname"] = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["member"]["nickname"]);
                HttpContext.Current.Session["username"] = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["member"]["username"]);
                HttpContext.Current.Session["mobile"] = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["member"]["mobile"]);
            }
        }
        if (HttpContext.Current.Session["mobile"] != null && HttpContext.Current.Session["memberid"] != null && HttpContext.Current.Session["nickname"] != null)
        {
            if (HttpContext.Current.Session["mobile"].ToString() == "")
            {
                DoneUserErr("请先登陆!");
            }
        }
        else
        {
            DoneUserErr("请先登陆!");
        }
    }

 

删除:

 Session["mobile"] = null;
        Session["memberid"] = null;
        Session["nickname"] = null;
        Session.Abandon();
        Session.Clear();
        if (Request.Cookies["member"] != null)
        {
            HttpCookie myCookie = new HttpCookie("member");
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie);
        }

posted @   94cool  阅读(384)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2009年7月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示