- public void SetMyCookie(string name, string vals,double extime)
- {
- HttpCookie usercookie = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(vals, System.Text.Encoding.GetEncoding("utf-8")));
- usercookie.Expires = DateTime.Now.AddDays(extime);
- HttpContext.Current.Response.Cookies.Add(usercookie);
- // HttpContext.Current.Response.Cookies.Add(new HttpCookie(name, System.Web.HttpUtility.UrlEncode(vals, System.Text.Encoding.GetEncoding("utf-8"))));
- }
3个参数, 有 cookie的 名 ,值 ,过期时间。
调用时 如下:
- SetMyCookie("Name","javc",1);
我们还需要一个取出cookie的函数 ,如下:
- public string GetMyCookie(string name)
- {
- if (HttpContext.Current.Request.Cookies[name] != null)
- {
- return System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[name].Value, System.Text.Encoding.GetEncoding("utf-8"));
- }
- else
- {
- //如果需要异常处理
- // Response.Write("<script type='text/javascript' language='javascript'>");
- // Response.Write("alert('请重新登陆!//n//n将被引导首页面');");
- // Response.Write(" parent.location='./login.aspx';");
- // Response.Write("</script>");
- // Response.End();
- //不需要 可自己返回空
- return "";
- }
- }