httpcookie对象,是成键值对出现的。
Cookie对象的常用的几个属性:
Name:获取或设置Cookie的名称
Expires:获取或设置Cookie的过期时间
Cookie对象的几个常用方法:
Add:新增一个Cookie对象
Clear:清楚会话状态中的所有数据
Remove:删除会话中的项
Get:通过变量名或索引得到Cookie的变量值
GetKey:以索引值来获取Cookie的变量名称
页面设计:
<table border="1" style=" 40%">
<tr>
<td colspan="2" style="text-align: center">
用户登录</td>
</tr>
<tr>
<td>
用户名:</td>
<td >
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td>
密 码:</td>
<td >
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td width="34%">
Cookie有效期:</td>
<td style=" 336px"><asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1" Selected="True">1天</asp:ListItem>
<asp:ListItem Value="2">1个月</asp:ListItem>
<asp:ListItem Value="3">半年</asp:ListItem>
<asp:ListItem Value="4">1年</asp:ListItem>
</asp:RadioButtonList></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="btnlog" runat="server" Text="登录" OnClick="btnlog_Click" />
<asp:Button ID="btnreg" runat="server" Text="注册" /></td>
</tr>
</table>
<tr>
<td colspan="2" style="text-align: center">
用户登录</td>
</tr>
<tr>
<td>
用户名:</td>
<td >
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td>
密 码:</td>
<td >
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td width="34%">
Cookie有效期:</td>
<td style=" 336px"><asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1" Selected="True">1天</asp:ListItem>
<asp:ListItem Value="2">1个月</asp:ListItem>
<asp:ListItem Value="3">半年</asp:ListItem>
<asp:ListItem Value="4">1年</asp:ListItem>
</asp:RadioButtonList></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="btnlog" runat="server" Text="登录" OnClick="btnlog_Click" />
<asp:Button ID="btnreg" runat="server" Text="注册" /></td>
</tr>
</table>
后台代码:

protected void btnlog_Click(object sender, EventArgs e)
{
string name = "admin";
string pass = "admin";
if (this.TextBox1.Text == "admin" && this.TextBox2.Text == "admin")
{
if (this.RadioButtonList1.SelectedValue.ToString() == "1")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddDays(1);
}
if (RadioButtonList1.SelectedValue.ToString() == "2")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(1);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddMonths(1);
}
if (RadioButtonList1.SelectedValue.ToString() == "3")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddYears(1 / 2);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddYears(1 / 2);
}
if (RadioButtonList1.SelectedValue.ToString() == "4")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddYears(1);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddYears(1);
}
this.Response.Cookies["UserName"].Value = this.TextBox1.Text;
this.Response.Cookies["Password"].Value = this.TextBox2.Text;
Response.Redirect("getcookie.aspx");
}
else
{
Response.Redirect("cookie.aspx");
}
}
{
string name = "admin";
string pass = "admin";
if (this.TextBox1.Text == "admin" && this.TextBox2.Text == "admin")
{
if (this.RadioButtonList1.SelectedValue.ToString() == "1")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddDays(1);
}
if (RadioButtonList1.SelectedValue.ToString() == "2")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(1);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddMonths(1);
}
if (RadioButtonList1.SelectedValue.ToString() == "3")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddYears(1 / 2);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddYears(1 / 2);
}
if (RadioButtonList1.SelectedValue.ToString() == "4")
{
this.Response.Cookies["UserName"].Expires = DateTime.Now.AddYears(1);
this.Response.Cookies["Password"].Expires = DateTime.Now.AddYears(1);
}
this.Response.Cookies["UserName"].Value = this.TextBox1.Text;
this.Response.Cookies["Password"].Value = this.TextBox2.Text;
Response.Redirect("getcookie.aspx");
}
else
{
Response.Redirect("cookie.aspx");
}
}
转到“getcookie.aspx”页面中
protected void Page_Load(object sender, EventArgs e)
{
this.lab.Text = "欢迎" + this.Request.Cookies["UserName"].Value + "回来";
}
{
this.lab.Text = "欢迎" + this.Request.Cookies["UserName"].Value + "回来";
}
由于并非所有的浏览器都支持Cookie,并且数据信息是以文本的形式保存在客户端机器中,因此最好不要保存敏感的、为加密的数据,否则会影响到网站的安全性。
string pass="admin";
Response.Cookies["pass"].Value=FormsAuthentication.HashPasswordForStoringInConfigFile(pass,"md5");
Response.Cookies["pass"].Value=FormsAuthentication.HashPasswordForStoringInConfigFile(pass,"md5");