ASP.NET Tutorial/Cookie/Delete

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Delete All Cookies

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Page_Load()
    {
        string[] cookies = Request.Cookies.AllKeys;
        foreach (string cookie in cookies)
        {
            BulletedList1.Items.Add("Deleting " + cookie);
            Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Delete All Cookies</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Delete All Cookies</h1>
    <asp:BulletedList
        id="BulletedList1"
        EnableViewState="false"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Delete Cookie by setting the date of Expires

<%@ Page %>
<script language="C#" runat="server">
private void Page_Init(object sender, EventArgs e)
{
  if((Request.Cookies["UserName"] != null) && 
    (Request.Cookies["UserName"].Value != null)) 
  UserNameLabel.Text = Request.Cookies["UserName"].Value.ToString();
}
private void SaveButton_Click(object sender, System.EventArgs e)
{
  Response.Cookies["UserName"].Value = UserNameTextBox.Text;
  if(PersistCookieCheckBox.Checked)
    Response.Cookies["UserName"].Expires = System.DateTime.Now.AddDays(1); 
  UserNameLabel.Text = UserNameTextBox.Text; 
}
private void DeleteButton_Click(object sender, System.EventArgs e)
{
  Response.Cookies["UserName"].Expires = System.DateTime.Now.AddDays(-1);
}
</script>
<html>
  <body>
    <form id="form1" method="post" runat="server">
      
        Update UserName in Cookie:
        <asp:TextBox id="UserNameTextBox" runat="server"></asp:TextBox>
        <asp:Button id="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click"></asp:Button>
        <asp:CheckBox id="PersistCookieCheckBox" runat="server" text="Persist Cookie?"></asp:CheckBox>
      
      
        Current Cookie Contents:
        <br>
        <asp:Label ID="UserNameLabel" Runat="server" EnableViewState="False"></asp:Label>
        <br>
        <asp:Button id="RefreshButton" runat="server" Text="Refresh Without Saving"></asp:Button>
        
        Deleting the cookie will take effect on the *next* postback, since the cookie information is still in Request.Cookies for the
        duration of the PostBack once the Delete button is clicked.  Click it once, then click the "Refresh Without Saving" button.
        
        <asp:Button id="DeleteButton" runat="server" Text="Delete Cookie" OnClick="DeleteButton_Click"></asp:Button>
      
    </form>
  </body>
</html>


Deleting Cookies

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Response.Cookies[txtCookieName.Text].Expires  = DateTime.Now.AddDays(-1);
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Delete Cookie</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblCookieName"
        Text="Cookie Name:"
        AssociatedControlID="txtCookieName"
        Runat="server" />
    <asp:TextBox
        id="txtCookieName"
        Runat="server" />
    <asp:Button
        id="btnDelete"
        Text="Delete Cookie"
        OnClick="btnDelete_Click"
        Runat="server" />
    </div>
    </form>
</body>
</html>