使用浏览器cookie
1.創建Cookie
<script runat="server">
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Cookies["message"].Value = txtCookieValue.Text;//對文本框輸入的內容創建Cookie
}
</script>
<form id="form1" runat="server">
<div >
<asp:Label ID="lblCookieValue" Text="Cookie Value:" AssociatedControlID="txtCookieValue" runat="server"/>
<asp:TextBox ID="txtCookieValue" runat="server" />
<asp:Button ID="btnAdd" Text="Add Value" OnClick="btnAdd_Click" runat="server" />
</div>
</form>
2.創建持久化Cookie
<script runat="server">
void Page_Load()
{
//Get current value of cookie
int counter = 0;
if (Request.Cookies["counter"] != null)
{
counter = Int32.Parse(Request.Cookies["counter"].Value);
//Increment counter
counter++;
//Add persistent cookie to browser
Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddYears(2);
//Display value of counter cookie
lblCounter.Text = counter.ToString();
}
}
</script>
?3.Cookie的屬性
HttpCookie類代表cookie。當創建或讀取一個cookie時,可以使用以下屬性
(1)Domain—用于指定關聯到cookie的域名,默認值是當前域名;
(2)Expires—用于通過指定一個過期時間創建一個持久化cookie;
(3)HasKeys—用于確定該cookie是否是一個多值的cookie;
(4)HttpOnly—用于避免cookie被Javascript訪問;
(5)Name—用戶指定cookie的名稱;
(6)Path—用于指定關聯到cookie的路徑。默認為/。
(7)Secure—用于指定cookie需要通過SSL連接傳遞;
(8)Value—允許讀/寫cookie的值;
(9)Values—當使用多值cookie時,用于讀/寫特定的值;
轉載于:https://www.cnblogs.com/wnntechnology/p/5527406.html
總結
以上是生活随笔為你收集整理的使用浏览器cookie的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: APP上架证书无效:解决
- 下一篇: 2017年html5行业报告,云适配发布