cookies的简单读写方法


一、cookie的写入

//定义COOKIES,实例化HttpCookie类并添加值

HttpCookie cookie = new HttpCookie(key, value); 

//设置保存时间  

cookie.Expires = DateTime.Now.AddDays(1);

//添加当前实例化的cookie

Response.Cookies.Add(cookie);


二、cookie的读取

public static string GetOwner()

{

   if (HttpContext.Current.Request.Cookies[key] == null)

   {

      throw new Exception("您没有登录系统或会话已过期,请重新登录");

    }

    else

    {

       //得到用户数据

       HttpCookie cook = HttpContext.Current.Request.Cookies[key];

       return HttpUtility.UrlDecode(cook.Value);

    }

}