Response.Cookies["userName"].Value = "admin";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
// 未設 Expires 則為 Session Cookie,瀏覽器關閉即失效
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
if (Request.Cookies["userName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);
if (Request.Cookies["userName"] != null) {
HttpCookie aCookie = Request.Cookies["userName"];
Label1.Text = Server.HtmlEncode(aCookie.Value);
}
Response.Cookies["userInfo"]["userName"] = "admin";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "admin";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
HttpCookie aCookie = Request.Cookies["userInfo"];
string userName = aCookie.Values["userName"];
string lastVisit = aCookie.Values["lastVisit"];
Cookie 無直接修改/刪除 API,皆透過**覆寫**實現:
Expires = DateTime.Now.AddDays(-1),瀏覽器會自動移除// 刪除範例
HttpCookie c = new HttpCookie("userName");
c.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(c);
HttpOnly=true 防止 XSS 竊取Secure=true 僅傳輸於加密連線