使用Cookies显示登录次数

       今天是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/>");

        }


    }

 


 

 

转载于:https://www.cnblogs.com/qihaiyan1989/archive/2010/04/28/1723083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值