ASP.NET用户重复登录控制

在使用ASP.NET进行Web开发时关于如何去掉用户重复登录,实现过程如下:

1、首先,对web.config配置如下:

<configuration>
 <system.web>
  <compilation debug="true" targetFramework="4.0">
   <assemblies>
    <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
  <customErrors defaultRedirect="~/ErrorPage.htm" mode="Off">
   <error statusCode="404" redirect="~/FileNotFind.aspx"></error>
  </customErrors>
    <authentication mode="Forms">
      <forms  loginUrl="default.aspx"></forms>
    </authentication>
    <authorization>
      <allow users="*" />
    </authorization>
    <globalization  requestEncoding="gb2312" responseEncoding="gb2312" />

    <sessionState mode="InProc" cookieless="true" timeout="5"></sessionState>
    <!--为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。-->
    <trace enabled="true"  requestLimit="1000"  pageOutput="true"  traceMode="SortByTime"  localOnly="true"/>
 </system.web>
  </configuration>

2、使用Global.asax全局控制文件,实现将登录的用户存储在application对象中,代码如下:

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码
        if (Application["online"] == null)
        {
            Hashtable h = new Hashtable();
            Application["online"] = h;
        }
      
    }
   
    void Application_End(object sender, EventArgs e)
    {
        //在应用程序关闭时运行的代码

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        //在会话结束时运行的代码。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

        LogoutCache();

    }

    /// <summary>/// 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用
    /// </summary>
    public void LogoutCache()
    {
        Hashtable h = (Hashtable)Application["online"];  
        if(h!=null)   
        {       
            if(h[Session.SessionID]!=null)           
                h.Remove(Session.SessionID);
            Application["online"] = h;   
        }
    }
      
</script>

3、用户登录界面,登录用能的实现:

//获取application系统对象中存储的在线用户 

Hashtable h = (Hashtable)Application["online"];
        string oldsessionid = null;
        if (h != null )   
        {       
            IDictionaryEnumerator e1 = h.GetEnumerator();       
            bool flag = false;       
            while (e1.MoveNext())       
            {           

                //判断当前登录用户时候存在于application对象中
                if (((string)((ArrayList)e1.Value)[0]).Equals(name))           
                {
                    flag = true;
                    oldsessionid = e1.Key.ToString();
                    break;           
                }       
            }
            if (flag)
            {

                //获取用户成功登录时间到目前现在时间差
                TimeSpan ts = System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[1]));
                if (ts.TotalSeconds < 300)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script> alert('对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!');</script>");
                    HttpSessionState sessionstate = (HttpSessionState)((ArrayList)e1.Value)[2];
                    //sessionstate.Clear();
                    sessionstate.Abandon();

                    try
                    {
                        throw new Exception("你的帐号已在别处登陆,你被强迫下线!");
                    }
                    catch (Exception)
                    {

                    }
                    finally
                    {
                        Response.Redirect("~/");
                    }
                }
                h.Remove(e1.Key);
            }

        }   
        else   
        {       
            h = new Hashtable();   
        }   
        ArrayList al = new ArrayList();   
        al.Add(name);    //当前登录的用户名
        al.Add(System.DateTime.Now);//登录的时间
        al.Add(HttpContext.Current.Session);//当前会话
        h[Session.SessionID] = al;
        Application["online"] = h;//将当前登录用户信息存入application中
       Response.Redirect("~/LoginSuccess.aspx");

 

如上只是部分代码,如果想下载全部代码,请到我的下载空间进行下载,谢谢大家支持!

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值