zoukankan      html  css  js  c++  java
  • asp.net中,登录互斥的相关代码(不包含中途退出的处理)

    孟子的代码,在其基础上修改的.
          // 作为唯一标识的Key,应该是唯一的,这可根据需要自己设定规则。
          
    // 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。 

          
    // 生成Key
          string sKey = UserName.Text + "_" + PassWord.Text;
          
    // 得到Cache中的给定Key的值
          string sUser = Convert.ToString(Cache[sKey]);
          
    // 检查是否存在
          if (sUser == null || sUser == String.Empty)
          
    {
            
    // Cache中没有该Key的项目,表名用户没有登录,或者已经登录超时
            
    // 注意下面使用的TimeSpan构造函数重载版本的方法,是进行是否登录判断的关键。
            TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
            HttpContext.Current.Cache.Insert(sKey,sKey,
    null,DateTime.MaxValue,SessTimeOut,
              System.Web.Caching.CacheItemPriority.NotRemovable,
    null);
            Session[
    "User"= sKey;
            
    // 首次登录,您可以做您想做的工作了。
            Msg.Text = "<h4 style='color:red'>嗨!欢迎您访问本站</h4>";
            
          }

          
    else if(Session["User"!= null && Session["User"].ToString() == sKey)
          
    {
    //这里要更新Session相关时间
              Msg.Text = "<h4 style='color:red'>已经登录</h4>";
          }

          
    else
          
    {
            
    // 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录
             Msg.Text="<h4 style='color:red'>抱歉,您好像已经在别的地方登录了呀:-(</h4>";
           
    return;
          }
  • 相关阅读:
    Hadoop专有名词
    mapreduce的cleanUp和setUp的特殊用法(TopN问题)和常规用法
    Hadoop 中文编码相关问题 -- mapreduce程序处理GBK编码数据并输出GBK编码数据
    MapReduce核心
    技术提升
    Linq使用group by
    C#中DllImport用法
    mysql分组后将未分组的列合并成行GROUP BY,GROUP_CONCAT
    MVC之CodeFirst
    MVC的Forms登录验证
  • 原文地址:https://www.cnblogs.com/sxlfybb/p/764892.html
Copyright © 2011-2022 走看看