• 获取用户登录次数(cookie)


    登录的时候先取cookie,取到就加1.然后保存。

    if (Request.Cookies["loginCount"] == null)
    {
    HttpCookie c= new HttpCookie("loginCount"); ;
    Response.Cookies["loginCount"].Value = "1";
    Response.Cookies["loginCount"].Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(c);
    }
    else
    {
    int count = Convert.ToInt32(Request.Cookies["loginCount"].Value) + 1;
    Response.Cookies["loginCount"].Value = count.ToString();
    }
    
     if (Request.Cookies["userCookie"] == null)
    {
    HttpCookie userCookie = new HttpCookie("userCookie");
    userCookie.Values["userName"] = userInfo.UserName.ToString();
    userCookie.Values["lastVist"] = DateTime.Now.ToString();
    userCookie.Values["count"] = "1";
    userCookie.Expires = DateTime.Now.AddDays(30);
    Response.Cookies.Add(userCookie);
    }
    else
    {
    int counter = Convert.ToInt32(Request.Cookies["userCookie"]["count"]) + 1;
    HttpCookie userCookie = new HttpCookie("userCookie");
    userCookie.Values["userName"] = userInfo.UserName.ToString();
    userCookie.Values["lastVist"] = DateTime.Now.ToString();
    userCookie.Values["count"] = counter.ToString();
    userCookie.Expires = DateTime.Now.AddDays(30);
    Response.Cookies.Add(userCookie);
    }
    在另一个页面取出来
    //读取Cookie
    string nameCookie = Request.Cookies["userCookie"]["userName"];
    Response.Write("用户名:" + nameCookie);
    string timeCookie = Request.Cookies["userCookie"]["lastVist"];
    Response.Write(" <br>上传访问时间:" + timeCookie);
    string countCookie = Request.Cookies["userCookie"]["count"];
    Response.Write(" <br>访问次数:" + countCookie); 

    防止同一账户重复登录

     

    放在登陆成功的地方:
     
    string key = TextBox1.Text; //用户名文本框设为cache关键字 
     string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
    if (uer == null || uer == String.Empty)//判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
    { 
      
    //定义cache过期时间 
      TimeSpan SessTimeout = new TimeSpan(00, System.Web.HttpContext.Current.Session.Timeout, 00);
    //第一次登陆的时候插入一个用户相关的cache值,
    HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null); 
    Session[
    "ADMINID"= TextBox1.Text; 
    Response.Redirect(
    "main.aspx");
    }
    else
    { 
    //重复登陆 Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
    }

  • 相关阅读:
    腰围2尺1,2,3,4,5,6,7,8寸各自等于是多少厘米/英寸(对比表)
    Android开发模板------自己定义SimpleCursorAdapter的使用
    怎样在多线程中使用JNI?
    UVa753/POJ1087_A Plug for UNIX(网络流最大流)(小白书图论专题)
    图解iPhone开发新手教程
    Why Hadoop2
    读完了csapp(中文名:深入理解计算机系统)
    hadoop备战:一台x86计算机搭建hadoop的全分布式集群
    Win7 公布网站 HTTP 错误 404.4
    Dump 文件生成与分析
  • 原文地址:https://www.cnblogs.com/zengxiangzhan/p/1630870.html
Copyright © 2020-2023  润新知