今天是4月28日,今天了解了关于Cookies的用法。面试官曾问我,viewstate 和session ,cookies区别。
今天对Cookies有了一点的认识。cookies是把数据存储在客户端。如果做网站的话,往往会用到谁登录,上次是什么时间登录的,这次登录的时间,登录多少次了。
今天就用 Cookies 来解决这个问题。这是我在其他的地方看到的。今天写在这里,只是想让自己更熟悉这个方法。
这是页面设计。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="密 码:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登陆" />
<asp:Button ID="Button2" runat="server" Text="取消" /></div>
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 登陆
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
String loginName = getName();
if (Request.Cookies[loginName] == null)
{
HttpCookie hcookie = new HttpCookie(loginName);
hcookie.Values["username"] = this.TextBox1.Text; //登录用户名
hcookie.Values["lastVist"] = DateTime.Now.ToString(); //上次访问时间
hcookie.Values["nowVist"] = DateTime.Now.ToString(); //本次访问时间
hcookie.Values["count"] = "1"; //登录用户访问次数
hcookie.Expires = DateTime.Now.AddDays(30); //设置保存时间是30天
Response.Cookies.Add(hcookie);
}
else
{
HttpCookie hcookie = new HttpCookie(loginName);
hcookie.Values["username"] = this.TextBox1.Text;
String lastVist = Request.Cookies[loginName]["nowVist"];
hcookie.Values["lastVist"] = lastVist;
hcookie.Values["nowVist"] = DateTime.Now.ToString();
hcookie.Values["count"] = (Convert.ToInt32(Request.Cookies[loginName]["count"].ToString()) + 1).ToString();
hcookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(hcookie);
}
Response.Redirect("Default2.aspx");
}
private String getName()
{
String uname = this.TextBox1.Text;
Session["uname"] = uname;
return uname;
}
}
Default2.aspx.cs的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["uname"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
String loginName = Session["uname"] as String;//这里两个String要大写。
Response.Write("登录用户是:" + Request.Cookies[loginName]["username"].ToString() + "<br/>");
Response.Write("本次登录时间是:" + Request.Cookies[loginName]["nowVist"].ToString() + "<br/>");
Response.Write("上次登录时间是:" + Request.Cookies[loginName]["lastVist"].ToString() + "<br/>");
Response.Write("当前用户登录次数是:" + Request.Cookies[loginName]["count"].ToString() + "<br/>");
}
}