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 @ 2009-07-14 11:11  94cool  阅读(388)  评论(0)    收藏  举报