這篇文章主要介紹了.net 添加Cookie的4種方法,有需要的朋友可以參考一下
代碼如下: //第一種添加Cookie方法 HttpCookie myCookie = new HttpCookie("userrole"); myCookie.Values["a"] = "a"; myCookie.Values[""] = "b"; myCookie.Expires.AddDays(1); Response.Cookies.Add(myCookie); //Response.AppendCookie(mycookie);這個也可以添加 //第一種獲取Cookie方法 Response.Write(Request.Cookies["userrole"].Values["a"].ToString()); //第二種添加Cookie方法 HttpCookie myCookie = new HttpCookie("userrole"); myCookie.Values["a"] = "a"; myCookie.Values["b"] = "b"; myCookie.Expires.AddDays(1); Response.Cookies.Add(myCookie); //第二種讀取Cookie方法 HttpCookie cookie = Request.Cookies["userrole"]; Response.Write(cookie.Values["a"].ToString()); Response.Write(cookie.Values["b"].ToString()); //第三種添加Cookie方法 HttpCookie myCookie = new HttpCookie("userrole"); myCookie.Value = "a"; Response.Cookies.Add(myCookie); //第三種讀取Cookie方法 Response.Write(Request.Cookies["userrole"].Value); //第四種添加Cookie方法 HttpContext.Current.Response.Cookies.Add(new HttpCookie("userrole", "超級管理員")); Response.Cookies["userrole"].Value = "超級管理員"; HttpCookie cookie = Request.Cookies["userrole"]; Response.Write(cookie.Value); //第三種讀取Cookie方法 Response.Write(Request.Cookies["userrole"].Value);