cookie的创建和读取简化版

创建:

HttpCookie cookie = new HttpCookie("user");


cookie.Values["stfId"] = CookieManager.Encrypt(staff.Id.ToString(),"ToCookie");
cookie.Values["dept"] = CookieManager.Encrypt(staff.Dept.DepartName, "ToCookie");
cookie.Values["name"] = CookieManager.Encrypt(staff.StfName, "ToCookie");

cookie.Expires = DateTime.Now.AddDays(1);
HttpContext.Current.Response.SetCookie(cookie);

 

 

//读取

public string GetCookie(string cookName,string key)
{
if (Request.Cookies[cookName] == null)
{
return "";
}

if (Request.Cookies[cookName].Values[key] != null)
{
return CookieManager.Decrypt(Request.Cookies[cookName].Values[key].ToString(), "ToCookie");
}
else
{
return "";
}
}

 

javjavascript cookie设置

function setcookie(name,days)[1]

{

var url = document.domain;

var exp = new Date();

exp.setTime(exp.getTime() + days*24*60*60*1000);

document.cookie = name + "="+ escape(url) +";expires="+ exp.toGMTString();

}

cookie取得

function getcookie(name)

{

var arr = document.cookie.match(new RegExp("[sS]*"+ name +"=([^;]*)"));

alert(arr);

if(arr != null)

return unescape(arr[1]);

return null;

}

 

posted @ 2014-02-16 17:05  武二郎  阅读(199)  评论(0编辑  收藏  举报