设为首页 加入收藏

TOP

Web应用中如何防止用户多次登录?
2014-11-23 22:55:16 来源: 作者: 【 】 浏览:2
Tags:Web 应用 如何 防止 用户 登录

1. 在用户登录时,判断此用户是否已经在Application中存在,如果存在就报错,不存在的话就加到Application中(Application是所有Session共有的,整个web应用程序唯一的一个对象) 代码如下:


string strUserId = txtUser.Text;


ArrayList list = Application.Get(“GLOBAL_USER_LIST”) as ArrayList;
if (list == null)
{
list = new ArrayList();
}
for (int i = 0; i < list.Count; i++)
{
if (strUserId == (list[i] as string))
{
//已经登录了,提示错误信息
lblError.Text = ”此用户已经登录”;
return;
}
}


list.Add(strUserId);
Application.Add(“GLOBAL_USER_LIST”, list);


在用户退出的时候将此用户从Application中去除,可以在Global.asax的Session_End事件中处理:


void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
string strUserId = Session["SESSION_USER"] as string;
ArrayList list = Application.Get(“GLOBAL_USER_LIST”) as ArrayList;
if (strUserId != null && list != null)
{
list.Remove(strUserId);
Application.Add(“GLOBAL_USER_LIST”, list);
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇国内一家大型网站的网络面试题 下一篇百度的一道笔试题(修改)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: