惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - 梵天

xp操作系统下对移动硬盘重新分区的方法 Discuz!6的论坛搭建图文教程 如何制定和编写软件项目计划 网页制作 JSP与ASP 的比较 WEB编程开发常用的代码 网页设计标准尺寸 网页设计中的页面尺寸标准 Asp编码优化技巧8则 IIS6.0下ASP的新增功能 asp采集程序原理 为何要学编程?如何学编程?用什么语言最好?有什么好书? 网络创业点选择越细越好 尽量不要做平台 个人网站风云榜:近三年草根站长名人录 做网站第一步 域名选择篇诠释 哪些个人主页可以向商业网站转型 - 梵天 准确预测热门关键词 - 梵天 个人站长做什么类型的站能实现盈利 - 梵天 投放Google AdWords的十二个小技巧 - 梵天 门户网站没有草根网站有前途 - 梵天
Asp.net中防止用户多次登录的方法
梵天 · 2008-02-03 · via 博客园 - 梵天
 

  在web开发时,有的系统要求同一个用户在同一时间只能登录一次,也就是如果一个用户已经登录了,在退出之前如果再次登录的话需要报错。

  常见的处理方法是,在用户登录时,判断此用户是否已经在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);

  当然这里使用Cache等保存也可以。

  接下来就是要在用户退出的时候将此用户从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);
  }
  }

  这些都没有问题,有问题的就是当用户直接点浏览器右上角的关闭按钮时就有问题了。因为直接关闭的话,并不会立即触发session过期事件,也就是关闭浏览器后再来登录就登不进去了。

  这里有两种处理方式:

  1、使用Javascript方式

  在每一个页面中加入一段Javascript代码:

以下是引用片段:
  function window.onbeforeunload()
  {
  if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey){
  window.open("logout.aspx");
  }
  }

  由于onbeforeunload方法在浏览器关闭、刷新、页面调转等情况下都会被执行,所以需要判断是点击了关闭按钮或是按下Alt+F4时才执行真正的关闭操作。

  然后在logout.aspx的Page_Load中写和session_End相同的方法,同时在logout.aspx中加入事件:onload="Javascript:window.close()"

  但是这样还是有问题,Javascript在不同的浏览器中可能有不同的行为,还有就是当通过文件->关闭时没有判断到。

  2、使用XMLhttp方法(这种方法测试下来没有问题)

  在每个页面中加入如下的javascript(这些Javascript也可以写在共通里,每个页面引入就可以了)

以下是引用片段:
  var x=0;
  function myRefresh()
  {
  var httpRequest = new ActiveXObject("microsoft.XMLhttp");
  httpRequest.open("GET", "test.aspx", false);
  httpRequest.send(null);
  x++;
  if(x<60) //60次,也就是session真正的过期时间是30分钟
  {
  setTimeout("myRefresh()",30*1000); //30秒
  }
  }
  myRefresh();

  在web.config中设置

以下是引用片段:
<sessionState mode="InProc" timeout="1"></sessionState> 

  test.aspx页面就是一个空页面,只不过需要在Page_Load中加入:

以下是引用片段:
  Response.Expires = -1;

  保证不使用缓存,每次都能调用到这个页面。

  原理就是:设置Session的过期时间是一分钟,然后在每个页面上定时每30秒连接一次测试页面,保持Session有效,总共连60次,也就是30分钟。如果30分钟后用户还没有操作,Session就会过期。当然,如果用户直接关闭浏览器,那么一分钟后session也会过期。这样就可以满足要求了。