- <%@ Page Language="C#" %>
- <%@ Import Namespace="System.Net" %>
-
- <!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 Page_Load(object sender, System.EventArgs e) {
- HttpCookie userCookie = new HttpCookie("UserInfo");
- userCookie["Country"] = "Italy";
- userCookie["City"] = "Rome";
- userCookie["Name"] = "Jones";
- userCookie.Expires = DateTime.Now.AddDays(3);
- Response.Cookies.Add(userCookie);
- Label1.Text = "Cookie create successful!<br><br/>";
-
- HttpCookie cookie = Request.Cookies["UserInfo"];
-
- if (cookie != null)
- {
- string country = cookie["Country"];
- string city = cookie["City"];
- string name = cookie["Name"];
- Label2.Text = "Cookie Found and read<br/>";
- Label2.Text += "Name: " + name;
- Label2.Text += "<br />Country: " + country;
- Label2.Text += "<br />City: " + city;
- }
- }
- </script>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>asp.net cookie example: how to create a cookie</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:Teal">asp.net Cookie example: Create a cookie</h2>
- <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="SeaGreen">
- </asp:Label>
- <asp:Label ID="Label2" runat="server" Font-Size="Large" ForeColor="Crimson">
- </asp:Label>
- </div>
- </form>
- </body>
- </html>