MVC、控件、一般处理程序中的session and cookie

8/10/2015来源:ASP.NET技巧人气:2011

MVC、控件、一般处理程序中的session and cookie

Mvc中:

session:

if (!string .IsNullOrEmpty(find))   //设置Session["oip"] = "无锡";ViewBag.oip =Session["oip"];if (Session["oip"] == null)         //获取Session["oip"] = null;              //设为nullSession.Timeout = 1;          //设置过期时间<sessionState mode="InPRoc" timeout="30"/>  //过期30分钟

cookie:

HttpCookie cookie = new HttpCookie("oip");cookie.Expires = System.DateTime.Now.AddYears(1);cookie["oipp"] = "用户名";      Response.Cookies.Add(cookie);        //设置<sessionState mode="InProc" timeout="60"/>  //设置过期时间cookie["oipp"] =null;HttpCookie cookiee = Response.Cookies.Get("oip");  //获取ViewBag.oip = cookiee["oipp"];

控件中:

 Session["UserInfo"]   //设置和获取
参考:http://www.cnblogs.com/kevin-top/archive/2010/07/04/1770726.htmlcookie:C# 计算时间差 用timespan函数http://www.blogjava.net/AndyZhang/archive/2012/05/02/377157.html
 HttpCookie cookie=new HttpCookie("MyCook");//初使化并设置Cookie的名称      //设置 DateTime dt=DateTime.Now; TimeSpan ts = new TimeSpan(0, 0, 1,0,0);//过期时间为1分钟 cookie.Expires = dt.Add(ts);//设置过期时间 cookie.Values.Add("userid", "userid_value"); cookie.Values.Add("userid2","userid2_value2"); Response.AppendCookie(cookie);
设置
if(Request.Cookies["MyCook"]!=null)                        //获取 {   //Response.Write("Cookie中键值为userid的值:" + Request.Cookies["MyCook"]["userid"]);//整行   //Response.Write("Cookie中键值为userid2的值" + Request.Cookies["MyCook"]["userid2"]);   Response.Write(Request.Cookies["MyCook"].Value);//输出全部的值 }
获取
//获取客户端的Cookie对象HttpCookie cok = Request.Cookies["MyCook"];                      //修改  新增 if (cok != null){    //修改Cookie的两种方法    cok.Values["userid"] = "alter-value";    cok.Values.Set("userid", "alter-value");    //往Cookie里加入新的内容     cok.Values.Set("newid", "newValue");     Response.AppendCookie(cok);}   
修改、新增
      HttpCookie cok = Request.Cookies["MyCook"];    if (cok != null)    {      if (!CheckBox1.Checked)      {        cok.Values.Remove("userid");//移除键值为userid的值      }      else      {        TimeSpan ts = new TimeSpan(-1, 0, 0, 0);        cok.Expires = DateTime.Now.Add(ts);//删除整个Cookie,只要把过期时间设置为现在      }      Response.AppendCookie(cok);    }
删除

一般处理程序:

session:

context.Session["ws_user"].ToString()Session["UserInfo"] = currentUser;

cookie:

HttpCookie cookie = HttpContext.Current.Request.Cookies["info"];// cookie = null;if (cookie == null ){cookie = new HttpCookie("Info");                               //设置cookie["CityID"] = HttpContext.Current.Server.UrlEncode(cityID);             //编码cookie["CityName"] = HttpContext.Current.Server.UrlEncode(CityName);cookie.Expires = DateTime.Now.AddDays(10);//HttpContext.Current.Response.Cookies.Add(cookie);}else{//直接读值,注意编码 解码、不然汉字会出现乱码。     Server.UrlDecode()}
# 用来标志特定的文档位置 %23 % 对特殊字符进行编码 %25 & 分隔不同的变量值对 %26 + 在变量值中表示空格 %2B \ 表示目录路径 %2F = 用来连接键和值 %3D ? 表示查询字符串的开始 %3F 

       HttpCookie cookie = new HttpCookie("Test");//初使化并设置Cookie的名称                 TimeSpan ts = new TimeSpan(0, 0, 1, 0, 0);//过期时间为1分钟        cookie.Expires = DateTime.Now.Add(ts);//设置过期时间        cookie.Values.Add("userid", "123456");        cookie.Values.Add("test", "THIS_IS_TEST");        context.Response.AppendCookie(cookie);         context.Response.Write(context.Request.Cookies["Test"].Value);   //获取