【HTTP与HTTPS】4.关于Cookie存在哪里的问题
程序员文章站
2022-05-15 18:23:06
...
首先Cookie失效分为两种:
(1)设置过期时间失效(只要设置了过期时间Cookie就会存储在硬盘里面)
(2)当会话结束时失效,即关闭浏览器窗口(如果没有设置Expires,Cookie就会存储在内存里面)
手动删除Cookie失效
下面给出个demo:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestCookie.aspx.cs" Inherits="LJWY_SP01_TestCookie" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>内存Cookie和硬盘Cookie</title>
<script type="text/javascript" runat=server>
//内存
protected void btn_cookie_1_Click(object sender, EventArgs e){
HttpCookie cookie=new HttpCookie("Memory");
cookie.Value = "内存存储";
HttpContext.Current.Response.AppendCookie(cookie);
}
//硬盘
protected void btn_cookie_2_Click(object sender, EventArgs e) {
HttpCookie cookie=new HttpCookie("Hard disk");
cookie.Value = "硬盘存储";
cookie.Expires=System.DateTime.Now.AddDays(1);
HttpContext.Current.Response.AppendCookie(cookie);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn_cookie_1" runat="server" Text="内存Cookie" onclick="btn_cookie_1_Click"/>
<asp:Button ID="btn_cookie_2" runat="server" Text="硬盘Cookie" onclick="btn_cookie_2_Click"/>
</div>
</form>
</body>
</html>
在浏览器中设置-->高级-->隐私设置与安全-->内容-->Cookie里面可以看到浏览器占存的Cookie