zoukankan      html  css  js  c++  java
  • 防止用户重复登录

    本例完成的功能就是防止用户重复登录!若用户已经登录,则当其再次登录时,弹出提示框后返回!

    实现思路:用户登录成功后,将用户登录信息存放到Hashtable类型的Application["Online"]里面,其键值为SessionID,其Value值为用户ID;当用户注销时,调用Session.Abandon;在Global.asax里面的SessionEnd事件中,将用户ID从Hashtable中删除;在用户访问页面时,察看Hashtable中是否有对应的用户ID如果没有则判断用户不在线(用户不在线的原因可能是按了注销按钮、网页超时等)

    1、公用类中判断用户是否在线的函数(供用户调用)
    /// <summary>
    /// 判断用户strUserID是否包含在Hashtable h中
    /// </summary>
    /// <param name="strUserID"></param>
    /// <param name="h"></param>
    /// <returns></returns>
    public static bool AmIOnline(string strUserID,Hashtable h)
    {
    if(strUserID == null)
    return false;

    //继续判断是否该用户已经登陆
    if(h == null)
    return false;

    //判断哈希表中是否有该用户
    IDictionaryEnumerator e1 = h.GetEnumerator();
    bool flag = false;
    while(e1.MoveNext())
    {
    if(e1.Value.ToString().CompareTo(strUserID) == 0)
    {
    flag = true;
    break;
    }
    }
    return flag;
    }

    2、用户登录事件处理:
    private void btnlogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    { ////User为自定义的类,其中包含Login方法
    User CurUser = new User();
    CurUser.UserID = this.username.Text.Trim();

    if(MyUtility.AmIOnline(CurUser.UserID,(Hashtable)Application["Online"]))
    {
    JScript.Alert("您所使用的登录ID已经在线了!您不能重复登录!");
    return;
    }

    CurUser.LoginPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(this.password.Text.Trim(),"SHA1");
    int ii = CurUser.Login();
    StringBuilder sbPmt = new StringBuilder();

    switch(ii)
    {
    case 0: //如果登录成功,则将UserID加入Application["Online"]中
    Hashtable h = (Hashtable)Application["Online"];
    if(h == null)
    h = new Hashtable();
    h[Session.SessionID] = CurUser.UserID;
    Application["Online"] = h;

    Session["UserID"] = CurUser.UserID;
    Session["UserNM"] = CurUser.UserNM;
    Session["RoleMap"] = CurUser.RoleMap;
    Session["LoginPsw"] = CurUser.LoginPsw;
    Session["LoginTime"] = DateTime.Now;
    Response.Redirect("ChooseRole.aspx");
    break;
    case -1:
    JScript.Alert("用户名错误!");
    break;
    case -2:
    JScript.Alert("密码错误!");
    break;
    default:
    sbPmt.Append("登录过程中发生未知错误!");
    JScript.Alert(sbPmt.ToString());
    break;
    }
    return;
    }

    3、在Global.asax中的Session_End事件:
    protected void Session_End(Object sender, EventArgs e)
    {
    Hashtable h=(Hashtable)Application["Online"];

    if(h[Session.SessionID]!=null)
    h.Remove(Session.SessionID);

    Application["Online"]=h;
    }

    4、在每一个页面需要刷新的地方,调用如下代码:
    try
    {
    if(!common.MyUtility.AmIOnline(Session["UserID"].ToString(),(Hashtable)Application["OnLine"]))
    {
    //用户没有在线 ,转到登录界面
    Response.Write("<script>parent.document.location.href='Login.aspx';</script>"); ////有框架时用
    //Response.Redirect("login.aspx"); ////无框架时用
    return;
    }
    }
    catch
    {
    //会话过期 ,转到登录界面
    Response.Write("<script>parent.document.location.href='Login.aspx';</script>"); ////有框架时所用
    //Response.Redirect("login.aspx"); ////无框架时用
    return;
    }

  • 相关阅读:
    Scrapy学习-18-去重原理
    Scrapy学习-17-暂停和重启
    Scrapy学习-16-动态网页技术
    Scrapy学习-15-降低被识别为爬虫的方法
    Scrapy学习-14-验证码识别
    Scrapy学习-13-使用DownloaderMiddleware设置IP代理池及IP变换
    Scrapy学习-12-使用DownloaderMiddleware随机修改User-Agent
    Scrapy学习-11-Selector对象使用
    使用grunt完成requirejs的合并压缩和js文件的版本控制
    nodemailer中的几个坑
  • 原文地址:https://www.cnblogs.com/juan/p/1431016.html
Copyright © 2011-2022 走看看