Cookie存取问题
获取
HttpCookie Cookie = CookiesHelper.GetCookie("UserInfo"); //有cookie值的话就取出来 if (Cookie != null) { this.txtUserName.Value = HttpUtility.UrlDecode(Cookie.Values["uName"]); this.txtPwd.Attributes.Add("value", Cookie.Values["Pwd"]); }
// 是否选中复选框(默认为true) if (this.chk.Checked == true) { //获得cookie HttpCookie Cookie = CookiesHelper.GetCookie("UserInfo"); //string cooke = HttpUtility.UrlDecode(CookiesHelper.GetCookie("UserInfo").Values["uName"]); if (Cookie == null) { Cookie = new HttpCookie("UserInfo"); Cookie.Values.Add("uName", HttpUtility.UrlEncode(uName)); Cookie.Values.Add("Pwd", Pwd); //设置Cookie过期时间 Cookie.Expires = DateTime.Now.AddDays(2); CookiesHelper.AddCookie(Cookie); } else if (!Cookie.Values["uName"].Equals(uName)) { CookiesHelper.SetCookie("UserInfo", "uName", uName); } Response.Redirect("~/Menu/Index_Menu.htm");
/// <summary> /// 添加Cookie /// </summary> /// <param name="cookie"></param> public static void AddCookie(HttpCookie cookie) { HttpResponse response = HttpContext.Current.Response; if (response != null) { //指定客户端脚本是否可以访问[默认为false] cookie.HttpOnly = true; //指定统一的Path,比便能通存通取 cookie.Path = "/"; //设置跨域,这样在其它二级域名下就都可以访问到了 //cookie.Domain = "chinesecoo.com"; response.AppendCookie(cookie); } }/// <summary> /// 设置Cookie子键的值 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> public static void SetCookie(string cookieName, string key, string value) { SetCookie(cookieName, key, value, null); }/// <summary> /// 设置Cookie /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expires"></param> public static void SetCookie(string cookieName, string key, string value, DateTime? expires) { HttpResponse response = HttpContext.Current.Response; if (response != null) { HttpCookie cookie = response.Cookies[cookieName]; if (cookie != null) { if (!string.IsNullOrEmpty(key) && cookie.HasKeys) cookie.Values.Set(key, value); else if (!string.IsNullOrEmpty(value)) cookie.Value = value; if (expires != null) cookie.Expires = expires.Value; response.SetCookie(cookie); } } }
function SetCk(){ if(document.getElementById("cbread").checked==true) { SetCookie(document.getElementById("tbLogName").value,document.getElementById("tbpwd").value); }}//写cookies函数function SetCookie(name,value){ var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();}function getCookie(name)//取cookies函数 { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return null;}function GetPwd(){ if(getCookie(document.getElementById("tbLogName").value)!=null) { document.getElementById("tbpwd").value=getCookie(document.getElementById("tbLogName").value); }}
[解决办法]
取Cookie一般使用
System.Web.HttpContext.Current.Request.Cookies[cookieName]
加Cookie一般是
System.Web.HttpContext.Current.Response.Cookies.Add()
[解决办法]
但是用户想换个账号登录 这时登录时
if (Cookie == null)//它就不为空了
那么就没添加cookie了
你应该登录成功的代码里面就应该删除Cookie或者重新设置Cookie,而不是在其他页面设置
也不是先判断再设置,是必须设置,不用判断
另外,你注销登录的时候也应该清空的
[解决办法]
Cookie更改了之后记得要add();