分享程序员开发的那些事...
首页
NTFS
漏洞
Android
Magento
服务器
Python
powershell
技术问答
Linux
网站首页
且构网 - 分享程序员编程开发的那些事
您所在的位置:
首页
>
利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
更新时间:2022-09-17 13:03:38
/*http://evlon.cnblogs.com/archive/2006/03/20/354191.html
*/
我在们使用ASP.Net开发WEB网站时,有的时候是不让同一个用户名在同一时间进行多次登陆的。
为了不影响原来的整个网站,我选择使用了HttpModuler来实现。
先让所有的Page从自己的Page类:BasePage类继承,并实现 ISigleLogin接口。相关代码如下:
public
interface
ISingleLogin
{
string
SigleUserLoginId
{
get
; }
void
SigleUserLogout();
}
public
class
BasePage : System.Web.UI.Page , BNet.Web.Modulers.ISingleLogin
{
public
BasePage()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
if
(Session[
"
UserId
"
]
==
null
)
{
Response.Write(
"
你还没有登陆
"
);
Response.Redirect(
"
login.aspx
"
);
}
}
ISingleLogin 成员
}
然后在Web.config中加入HttpModuler:
<
system
.web
>
<
httpModules
>
<
add
name
="SingleLogin"
type
="BNet.Web.Modulers.SingleLoginModuler"
/>
</
httpModules
>
</
system.web
>
相关的SigleLoginModuler代码如下:
using
System;
using
System.Collections.Generic;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.UI;
namespace
BNet.Web.Modulers
{
///
<summary>
///
SingleLoginModuler 的摘要说明
///
</summary>
public
class
SingleLoginModuler : System.Web.IHttpModule
{
const
string
sigle_login_userid
=
"
evlon_siglelogin_userid
"
;
const
string
sigle_pre_logout_sessionid
=
"
evlon_sigle_pre_logout_sessionid
"
;
static
StringLifeValueDictionary loginedUserIdDictionary
=
null
;
static
StringLifeValueDictionary LoginedUserIdDictionary
{
get
{
if
(loginedUserIdDictionary
==
null
)
{
loginedUserIdDictionary
=
new
StringLifeValueDictionary();
}
else
{
List
<
string
>
listRemove
=
new
List
<
string
>
();
StringLifeValueDictionary.Enumerator iter
=
loginedUserIdDictionary.GetEnumerator();
while
(iter.MoveNext())
{
if
(iter.Current.Value.life
<
DateTime.Now)
{
listRemove.Add(iter.Current.Key);
}
}
foreach
(
string
key
in
listRemove)
{
loginedUserIdDictionary.Remove(key);
}
}
return
loginedUserIdDictionary;
}
}
static
StringLifeValueDictionary preLogoutSessionIdDictionary
=
null
;
static
StringLifeValueDictionary PreLogoutSessionIdDictionary
{
get
{
if
(preLogoutSessionIdDictionary
==
null
)
{
preLogoutSessionIdDictionary
=
new
StringLifeValueDictionary();
}
else
{
List
<
string
>
listRemove
=
new
List
<
string
>
();
StringLifeValueDictionary.Enumerator iter
=
preLogoutSessionIdDictionary.GetEnumerator();
while
(iter.MoveNext())
{
if
(iter.Current.Value.life
<
DateTime.Now)
{
listRemove.Add(iter.Current.Key);
}
}
foreach
(
string
key
in
listRemove)
{
preLogoutSessionIdDictionary.Remove(key);
}
}
return
preLogoutSessionIdDictionary;
}
}
public
SingleLoginModuler()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
IHttpModule 成员
}
public
class
LifeValue
{
public
string
value;
public
DateTime life;
public
LifeValue(
string
value)
{
this
.value
=
value;
this
.life
=
DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout
+
5
);
}
}
public
class
StringLifeValueDictionary : Dictionary
<
string
, LifeValue
>
{
}
public
interface
ISingleLogin
{
string
SigleUserLoginId
{
get
; }
void
SigleUserLogout();
}
}
本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2006/03/21/354569.html,如需转载请自行联系原作者
上一篇 : :【彩票】彩票预测算法(一):离散型马尔可夫链模型C#实现
下一篇 : 用HttpWebRequest抓取网页,"尝试自动重定向的次数太多”的错误,
相关阅读
利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
推荐文章
大数据应用还处于早期——专访Hadoop之父Doug Cutting
IBM建立大中华区云计算中心供验证测试
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 12 章 全文搜索_12.5. 解析器
赛思信安助力国家档案局丰富和完善“中国记忆”
SUSE营销副总裁:大数据,正大行其道
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 12 章 全文搜索_12.6. 词典
SaaS的中国式成功 中企开源服务保障体系揭秘
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 11 章 索引_11.9. 操作符类和操作符族
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 11 章 索引_11.8. 部分索引
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 14 章 性能提示_14.4. 填充一个数据库