ASP.NET
.................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookie.aspx.cs" Inherits="Cookie" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Username:<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox><br />
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<asp:CheckBox ID="chkremem" Text="Remember me next time?" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
...............
C#.NET
.........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Cookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
HttpCookie un = Request.Cookies["Username"];
HttpCookie pw = Request.Cookies["Password"];
if (un.Value != "" && pw.Value != "")
{
string usname = un.Value;
string passw = pw.Value;
txtUsername.Text = usname;
txtPassword.Text = passw;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (chkremem.Checked == true)
{
HttpCookie myCookie = new HttpCookie("Username");
HttpCookie myCookies = new HttpCookie("Password");
myCookie.Value = txtUsername.Text;
myCookies.Value = txtPassword.Text;
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);
Response.Cookies.Add(myCookies);
Response.Cookies["Username"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(1);
}
}
}