Cookie

Cookie (печенье) — это промежуточная временная текстовая информация, которая хранится в браузере пользователя.
Размер cookies не должен превышать более 4кб.
 
 

одноразовая кука

Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text="Name : "></asp:Label>
   &nbsp;&nbsp;
   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   &nbsp;&nbsp;
   <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" Width="54px" />
   <br />
   <br />
   <a target="_blank" href="a.aspx">go to a page of a</a>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
 
 }
 protected void Button1_Click(object sender, EventArgs e) {
  //создаем объект
  HttpCookie cookie1 = new HttpCookie("cookie1", TextBox1.Text);
  //записываем в коллекцию
  Response.Cookies.Add(cookie1);
 }
}
Скрыть

Показать

Копировать
  a.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  a.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class a : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
  /*вариант без проверки*/
  //Label1.Text = "Hello " + Request.Cookies["cookie1"].Value + " !";
 
  /*вариант с проверкой*/
  HttpCookie cookie1 = Request.Cookies["cookie1"];
  if(cookie1 != null) {
   Label1.Text = "Hello " + cookie1.Value + " !";
  }
  else {
   Label1.Text = string.Empty;
  }
 }
}
 
 

кука с установленным временем

Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text="Name : "></asp:Label>
   &nbsp;&nbsp;
   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   &nbsp;&nbsp;
   <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" Width="54px" />
   <br />
   <br />
   <a target="_blank" href="a.aspx">go to a page of a</a>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
 
 }
 protected void Button1_Click(object sender, EventArgs e) {
  //создаем объект
  HttpCookie cookie1 = new HttpCookie("cookie1", TextBox1.Text);
  //время жизни куки, к нынешней дате добавляем год
  cookie1.Expires = DateTime.Now.AddDays(365);
  //записываем в коллекцию
  Response.Cookies.Add(cookie1);
 }
}
Скрыть

Показать

Копировать
  a.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  a.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class a : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
  /*вариант без проверки*/
  //Label1.Text = "Hello " + Request.Cookies["cookie1"].Value + " !";
 
  /*вариант с проверкой*/
  HttpCookie cookie1 = Request.Cookies["cookie1"];
  if(cookie1 != null) {
   Label1.Text = "Hello " + cookie1.Value + " !";
  }
  else {
   Label1.Text = string.Empty;
  }
 }
}
 
 

удаление куки программно

Скрыть

Показать

Копировать
  Default.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text="Name : "></asp:Label>
   &nbsp;&nbsp;
   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   &nbsp;&nbsp;
   <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" Width="54px" />
   &nbsp;&nbsp;
   <asp:Button ID="Button2" runat="server" Text="Delete" OnClick="Button2_Click" />
   <br />
   <br />
   <a target="_blank" href="a.aspx">go to a page of a</a>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  Default.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
 
 }
 //OK
 protected void Button1_Click(object sender, EventArgs e) {
  //создаем объект
  HttpCookie cookie1 = new HttpCookie("cookie1", TextBox1.Text);
  //время жизни куки, к нынешней дате добавляем год
  cookie1.Expires = DateTime.Now.AddDays(365);
  //записываем в коллекцию
  Response.Cookies.Add(cookie1);
 }
 //Delete
 protected void Button2_Click(object sender, EventArgs e) {
  //создаем объект
  HttpCookie cookie1 = new HttpCookie("cookie1");
  //создаем прошедшую дату
  cookie1.Expires = DateTime.Now.AddDays(-1);
  //записываем в коллекцию
  Response.Cookies.Add(cookie1);
  //в ответе браузеру, заставляем его сделать запрос на эту страницу
  Response.Redirect(Request.Url.PathAndQuery);
 }
}
Скрыть

Показать

Копировать
  a.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <meta charset="utf-8" />
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
  </div>
 </form>
</body>
</html>
Скрыть

Показать

Копировать
  a.aspx.cs  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class a : System.Web.UI.Page {
 protected void Page_Load(object sender, EventArgs e) {
  /*вариант без проверки*/
  //Label1.Text = "Hello " + Request.Cookies["cookie1"].Value + " !";
 
  /*вариант с проверкой*/
  HttpCookie cookie1 = Request.Cookies["cookie1"];
  if(cookie1 != null) {
   Label1.Text = "Hello " + cookie1.Value + " !";
  }
  else {
   Label1.Text = string.Empty;
  }
 }
}