无法获取到Cookie
有两个项目的帐号的登录状态需要同步。所以使用Cookie来保持登录状态,对方是Java平台,由那一方写入cookie,ASP.NET来读,但是我这边却怎么也读取不到。
string value = string.Empty;if (HttpContext.Current.Request.Cookies["Mall_IsLogin"] != null){value = HttpContext.Current.Request.Cookies["Mall_IsLogin"].Value;if (!string.IsNullOrEmpty(value)){ //需要执行的代码}
protected void Button1_Click(object sender, EventArgs e) { HttpCookie objCookie = new HttpCookie("Mall_IsLogin"); objCookie.Value = "高丽佳33"; DateTime dtExpiry = DateTime.Now.AddDays(10); objCookie.Expires = dtExpiry; objCookie.Domain = ".h2h.cn"; if (this.Response.Cookies["Mall_IsLogin"] == null) { Response.Cookies.Add(objCookie); } else { this.Response.Cookies.Remove("Mall_IsLogin"); this.Response.Cookies.Set(objCookie); } } protected void Button2_Click(object sender, EventArgs e) { if (Request.Cookies["Mall_IsLogin"] != null) { string v = Request.Cookies["Mall_IsLogin"].Value; Button2.Text = v; } }
if (Request.Cookies["Mall_IsLogin"] != null) { string v = Request.Cookies["Mall_IsLogin"].Value; Button2.Text = v; }
[解决办法]
你的测试代码存在两个问题
1.下面的代码永远不会执行,因为Response.Cookies["Mall_IsLogin"]!=null的
if (this.Response.Cookies["Mall_IsLogin"] == null) { Response.Cookies.Add(objCookie); }