我在们使用ASP.Net开发WEB网站时,有的时候是不让同一个用户名在同一时间进行多次登陆的。
为了不影响原来的整个网站,我选择使用了HttpModuler来实现。
先让所有的Page从自己的Page类:BasePage类继承,并实现 ISigleLogin接口。相关代码如下:
public interface ISingleLogin
{
string SigleUserLoginId { get; }
void SigleUserLogout();
}
{
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");
}
}
#region ISingleLogin 成员
public string SigleUserLoginId
{
get
{
if (Session["UserId"] != null)
{
return Session["UserId"].ToString();
}
else
return "";
}
}
public void SigleUserLogout()
{
Session.Abandon();
Response.Write("你在别处已经登陆,强制退出本次登陆!");
}
#endregion
}
{
public BasePage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (Session["UserId"] == null)
{
Response.Write("你还没有登陆");
Response.Redirect("login.aspx");
}
}
#region ISingleLogin 成员
public string SigleUserLoginId
{
get
{
if (Session["UserId"] != null)
{
return Session["UserId"].ToString();
}
else
return "";
}
}
public void SigleUserLogout()
{
Session.Abandon();
Response.Write("你在别处已经登陆,强制退出本次登陆!");
}
#endregion
}
然后在Web.config中加入HttpModuler:
<system.web>
<httpModules>
<add name="SingleLogin" type="BNet.Web.Modulers.SingleLoginModuler"/>
</httpModules>
</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";
public static StringLifeValueDictionary UsableGetter(ref StringLifeValueDictionary dic)
{
if (dic == null)
{
dic = new StringLifeValueDictionary();
}
else
{
List<string> listRemove = new List<string>();
StringLifeValueDictionary.Enumerator iter = dic.GetEnumerator();
while (iter.MoveNext())
{
if (iter.Current.Value.life < DateTime.Now)
{
listRemove.Add(iter.Current.Key);
}
}
foreach (string key in listRemove)
{
dic.Remove(key);
}
}
return dic;
}
static StringLifeValueDictionary loginedUserIdDictionary = null;
static StringLifeValueDictionary LoginedUserIdDictionary
{
get
{
return UsableGetter(ref loginedUserIdDictionary);
}
}
static StringLifeValueDictionary preLogoutSessionIdDictionary = null;
static StringLifeValueDictionary PreLogoutSessionIdDictionary
{
get
{
return UsableGetter(ref preLogoutSessionIdDictionary);
}
}
public SingleLoginModuler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpModule 成员
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication context = sender as HttpApplication;
IHttpHandler httpHandler = context.Context.CurrentHandler;
ISingleLogin sl = httpHandler as ISingleLogin;
if (sl != null)
{
string suid = sl.SigleUserLoginId;
if (suid != string.Empty)
{
if (PreLogoutSessionIdDictionary.ContainsKey(context.Session.SessionID))
{
//这个用户应该强制登出
PreLogoutSessionIdDictionary.Remove(context.Session.SessionID);
Page page = (Page)httpHandler;
page.PreInit += new EventHandler(page_PreInit);
}
else if (!LoginedUserIdDictionary.ContainsKey(suid))
{
LoginedUserIdDictionary.Add(suid, new LifeValue(context.Session.SessionID));
}
}
}
}
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
ISingleLogin sl = page as ISingleLogin;
if (sl != null)
{
sl.SigleUserLogout();
page.Response.End();
}
}
void context_PostRequestHandlerExecute(object sender, EventArgs e)
{
//从LogineduserId 里找到和当前用户一样的用户ID的SessionId
HttpApplication context = sender as HttpApplication;
IHttpHandler httpHandler = context.Context.CurrentHandler;
ISingleLogin sl = httpHandler as ISingleLogin;
if (sl != null)
{
string suid = sl.SigleUserLoginId;
if (suid != string.Empty)
{
if (LoginedUserIdDictionary.ContainsKey(suid))
border-right: #808080 1px solid; border-top: #808080 1px so
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";
public static StringLifeValueDictionary UsableGetter(ref StringLifeValueDictionary dic)
{
if (dic == null)
{
dic = new StringLifeValueDictionary();
}
else
{
List<string> listRemove = new List<string>();
StringLifeValueDictionary.Enumerator iter = dic.GetEnumerator();
while (iter.MoveNext())
{
if (iter.Current.Value.life < DateTime.Now)
{
listRemove.Add(iter.Current.Key);
}
}
foreach (string key in listRemove)
{
dic.Remove(key);
}
}
return dic;
}
static StringLifeValueDictionary loginedUserIdDictionary = null;
static StringLifeValueDictionary LoginedUserIdDictionary
{
get
{
return UsableGetter(ref loginedUserIdDictionary);
}
}
static StringLifeValueDictionary preLogoutSessionIdDictionary = null;
static StringLifeValueDictionary PreLogoutSessionIdDictionary
{
get
{
return UsableGetter(ref preLogoutSessionIdDictionary);
}
}
public SingleLoginModuler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpModule 成员
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication context = sender as HttpApplication;
IHttpHandler httpHandler = context.Context.CurrentHandler;
ISingleLogin sl = httpHandler as ISingleLogin;
if (sl != null)
{
string suid = sl.SigleUserLoginId;
if (suid != string.Empty)
{
if (PreLogoutSessionIdDictionary.ContainsKey(context.Session.SessionID))
{
//这个用户应该强制登出
PreLogoutSessionIdDictionary.Remove(context.Session.SessionID);
Page page = (Page)httpHandler;
page.PreInit += new EventHandler(page_PreInit);
}
else if (!LoginedUserIdDictionary.ContainsKey(suid))
{
LoginedUserIdDictionary.Add(suid, new LifeValue(context.Session.SessionID));
}
}
}
}
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
ISingleLogin sl = page as ISingleLogin;
if (sl != null)
{
sl.SigleUserLogout();
page.Response.End();
}
}
void context_PostRequestHandlerExecute(object sender, EventArgs e)
{
//从LogineduserId 里找到和当前用户一样的用户ID的SessionId
HttpApplication context = sender as HttpApplication;
IHttpHandler httpHandler = context.Context.CurrentHandler;
ISingleLogin sl = httpHandler as ISingleLogin;
if (sl != null)
{
string suid = sl.SigleUserLoginId;
if (suid != string.Empty)
{
if (LoginedUserIdDictionary.ContainsKey(suid))
border-right: #808080 1px solid; border-top: #808080 1px so
相关推荐
一个JavaWeb小例子,实现一账号同一时间只能登录一次。附完整代码和数据库。
此程序为个人改写老师的实例所得,不收任何资源分,仅供大家参考
JAVAWEB_(实例讲解)登陆程序实现
利用钩子函数轻松实现应用程序的汉化C++源代码程序小实例
通过多个典型工程实例对上述三个层次的开发技术和编程技巧进行讲解,并对同一实例的多个解决方案进行对比分析,切实帮助开发人员提高JAVA EE Web开发水平。 除了对JAVA EE Web编程基本技术的讲解淙外,还针对Web...
VB中实现同一窗口的多个实例及控件的动态增减
在同一个页面应用多种TAB的综合实例,可在一个页面中同时布局选项卡,也布局滑动门,或者是二者兼顾。这个问题以前用户就提到过,不知如何在同一页面布置多个选项卡,你可以参考一下这个。 1、支持一个页面多次...
实例114 封装类实现一个简单的计算器 139 实例115 使用面向对象思想查找字符串中的 所有数字 140 5.3 面向对象技术高级应用 141 实例116 自定义抽象类计算圆形的面积 141 实例117 利用接口实现选择不同的语言 143 ...
一个oracle用户,创建多个实例图解。
C++程序实例,很基础的C++的例子 C++程序实例,很基础的C++的例子
javaweb三层架构实现用户登录实例.rar
同一页面多个Tab选项卡嵌套显示实例,可用于网页导航。
在连接两个VLAN的运营商...由于两个VLAN ID不一致,但是两个VLAN又属于同一个网段。用2台交换机模拟运行商,2台交换机模拟总公司和分公司交换网络,通过配置VLAN Mapping跨运营商网络实现同一网段不同VLAN的主机通信。
本书将重点放在实践上,围绕一个高度交互的web应用程序实例一步一步讲述了开发、调试、部署的每一个阶段。其中前两章讲述了WEB环境和web开发的基础切识,在随后的11章中从理论和实践两个方而讲解了web应用程序的创建...
[Android][同一对象不同实例同步操作一个目标]
利用DirectSound实现WAVE的播放C++源代码程序小实例visual studio.net
实例66 如何编写用户控件程序 实例67 如何给程序打包 第4章 系统维护 ---------------------------------- 实例68 如何获取系统基本信息 实例69 如何获取系统服务信息 实例70 如何获取系统进程信息 实例71 如何获取...
萨尔瓦托里在多租户AKS集群中使用Application Gateway Ingress Controller 此示例说明如何在多租户AKS群集中使用Application Gateway Ingress Controller来公开同一应用程序的多个实例,每个实例使用不同的子域公开...
JBOSS4.2 自定义服务器(同一IP启动多个实例) 配置方法
实例2-1:演示C++程序的运行步骤。源代码在光盘中“\02\firstcpp”目录下。 实例2-2:Visual C++语言基本元素介绍。源代码在光盘中“\02\secondcpp”目录下。 实例2-3:cout流控制符setw的使用。源代码在光盘中“\02...