zoukankan      html  css  js  c++  java
  • 妙用Cache检验用户是否重复登陆

    //生成Key   
                string sKey = TextBox1.Text + "_IsLogin";
                
    //得到Cache中的给定Key的值   
                string sUser = Convert.ToString(Cache[sKey]);

                
    //检查是否存在   
                if (sUser == null || sUser == String.Empty)
                
    {
                    
    //Cache中没有该Key的项目,表明用户没有登录,或者已经登录超时      
                    
    //TimeSpan 表示一个时间间隔,获取系统对session超时作的设置值
                    
    //TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
                    
    //(如果考虑到允许用户再次登陆的时间小于session超时时间,可将此值设小,在此示例中设置为一分钟) 
                    TimeSpan SessTimeOut = new TimeSpan(00100);
                    HttpContext.Current.Cache.Insert(sKey, sKey, 
    null, DateTime.MaxValue, SessTimeOut,
                                                     System.Web.Caching.CacheItemPriority.NotRemovable, 
    null);
                    
    //首次登录,您可以做您想做的工作了。   
                    Label1.Text = "你好!欢迎光临";
                }

                
    else
                
    {
                    
    //在Cache中发现该用户的记录,表名已经登录过,禁止再次登录   
                    Label1.Text = "对不起,你已在别处登陆.或者在1分钟后重试";
                    
    return;
                }
  • 相关阅读:
    MySQL创建数据库简单命令
    工作的本质是解决问题
    使用消息中间件时,如何保证消息仅仅被消费一次?
    缓存穿透了怎么办?
    MySQL 数据库的提速器-写缓存(Change Buffer)
    删库了,我们一定要跑路吗?
    做好一件事的三要素
    一分钟简单了解 JSON Web Token
    聊一聊 MySQL 中的数据编辑过程中涉及的两阶段提交
    聊一聊 MySQL 数据库中的那些锁
  • 原文地址:https://www.cnblogs.com/hyd309/p/1440626.html
Copyright © 2011-2022 走看看