利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆

简介:

/*http://evlon.cnblogs.com/archive/2006/03/20/354191.html
ExpandedSubBlockEnd.gif
*/


我在们使用ASP.Net开发WEB网站时,有的时候是不让同一个用户名在同一时间进行多次登陆的。
      为了不影响原来的整个网站,我选择使用了HttpModuler来实现。

      先让所有的Page从自己的Page类:BasePage类继承,并实现 ISigleLogin接口。相关代码如下:

None.gif      public   interface  ISingleLogin
ExpandedBlockStart.gif    
{
ExpandedSubBlockStart.gif        
string SigleUserLoginId get; }
InBlock.gif
InBlock.gif        
void SigleUserLogout();
InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif public   class  BasePage : System.Web.UI.Page , BNet.Web.Modulers.ISingleLogin
ExpandedBlockStart.gif
{
InBlock.gif    
public BasePage()
ExpandedSubBlockStart.gif    
{
InBlock.gif        
//
InBlock.gif        
// TODO: 在此处添加构造函数逻辑
InBlock.gif        
//
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
protected override void OnLoad(EventArgs e)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
base.OnLoad(e);
InBlock.gif        
if (Session["UserId"== null)
ExpandedSubBlockStart.gif        
{
InBlock.gif            Response.Write(
"你还没有登陆");
InBlock.gif            Response.Redirect(
"login.aspx");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gif    
ISingleLogin 成员
ExpandedBlockEnd.gif}

None.gif

      然后在Web.config中加入HttpModuler:

None.gif < system .web >
None.gif    
< httpModules >
None.gif      
< add  name ="SingleLogin"  type ="BNet.Web.Modulers.SingleLoginModuler" />
None.gif
None.gif    
</ httpModules >
None.gif
</ system.web >
None.gif
      相关的SigleLoginModuler代码如下:
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.UI;
None.gif
None.gif
namespace  BNet.Web.Modulers
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// SingleLoginModuler 的摘要说明
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SingleLoginModuler : System.Web.IHttpModule
ExpandedSubBlockStart.gif    
{
InBlock.gif        
const string sigle_login_userid = "evlon_siglelogin_userid";
InBlock.gif        
const string sigle_pre_logout_sessionid = "evlon_sigle_pre_logout_sessionid";
InBlock.gif
InBlock.gif        
static StringLifeValueDictionary loginedUserIdDictionary = null;
InBlock.gif        
static StringLifeValueDictionary LoginedUserIdDictionary
ExpandedSubBlockStart.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if (loginedUserIdDictionary == null)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    loginedUserIdDictionary 
= new StringLifeValueDictionary();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gif                
{
InBlock.gif                    List
<string> listRemove = new List<string>();
InBlock.gif                    StringLifeValueDictionary.Enumerator iter 
= loginedUserIdDictionary.GetEnumerator();
InBlock.gif                    
while (iter.MoveNext())
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        
if (iter.Current.Value.life < DateTime.Now)
ExpandedSubBlockStart.gif                        
{
InBlock.gif                            listRemove.Add(iter.Current.Key);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
foreach (string key in listRemove)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        loginedUserIdDictionary.Remove(key);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return loginedUserIdDictionary;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static StringLifeValueDictionary preLogoutSessionIdDictionary = null;
InBlock.gif        
static StringLifeValueDictionary PreLogoutSessionIdDictionary
ExpandedSubBlockStart.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if (preLogoutSessionIdDictionary == null)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    preLogoutSessionIdDictionary 
= new StringLifeValueDictionary();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gif                
{
InBlock.gif                    List
<string> listRemove = new List<string>();
InBlock.gif                    StringLifeValueDictionary.Enumerator iter 
= preLogoutSessionIdDictionary.GetEnumerator();
InBlock.gif                    
while (iter.MoveNext())
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        
if (iter.Current.Value.life < DateTime.Now)
ExpandedSubBlockStart.gif                        
{
InBlock.gif                            listRemove.Add(iter.Current.Key);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
foreach (string key in listRemove)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        preLogoutSessionIdDictionary.Remove(key);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return preLogoutSessionIdDictionary;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SingleLoginModuler()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gif        
IHttpModule 成员
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class LifeValue
ExpandedSubBlockStart.gif    
{
InBlock.gif        
public string value;
InBlock.gif        
public DateTime life;
InBlock.gif
InBlock.gif        
public LifeValue(string value)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
this.value = value;
InBlock.gif            
this.life = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout + 5);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class StringLifeValueDictionary : Dictionary<string, LifeValue>
ExpandedSubBlockStart.gif    
{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public interface ISingleLogin
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
string SigleUserLoginId get; }
InBlock.gif
InBlock.gif        
void SigleUserLogout();
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2006/03/21/354569.html,如需转载请自行联系原作者
相关文章
|
2月前
|
SQL 安全 测试技术
Web应用程序安全测试
Web应用程序安全测试
|
3月前
|
存储 缓存 监控
Web 应用程序性能测试核心步骤
Web 应用程序性能测试核心步骤
|
3月前
|
缓存 前端开发 JavaScript
构建高性能Web应用程序的关键技术
本文探讨了构建高性能Web应用程序所需的关键技术,包括前端和后端开发、Java、Python、C等编程语言的选择,以及数据库的优化。通过深入了解这些技术,开发人员可以提高应用程序的性能和用户体验。
29 2
|
3月前
|
前端开发 Java 开发者
Spring MVC:构建高效、可维护、可扩展的Web应用程序
Spring MVC:构建高效、可维护、可扩展的Web应用程序
27 0
|
1月前
|
缓存 关系型数据库 API
后端开发:构建高效、可扩展的Web应用程序的关键
后端开发:构建高效、可扩展的Web应用程序的关键
20 0
|
1月前
|
SQL 安全 测试技术
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
如何在 Python 中进行 Web 应用程序的安全性管理,例如防止 SQL 注入?
15 0
|
1月前
|
存储 安全 数据安全/隐私保护
什么是 Web 应用程序的会话管理?如何在 Python 中实现?
什么是 Web 应用程序的会话管理?如何在 Python 中实现?
10 2
|
1月前
|
存储 设计模式 前端开发
请解释 Web 应用程序的 MVC(模型-视图-控制器)架构。
【2月更文挑战第26天】【2月更文挑战第89篇】请解释 Web 应用程序的 MVC(模型-视图-控制器)架构。
|
1月前
|
Web App开发 前端开发 测试技术
Web应用程序测试工具Selenium用法详解
Web应用程序测试工具Selenium用法详解
37 0
|
2月前
|
API 网络架构
解释 RESTful API,以及如何使用它构建 web 应用程序。
解释 RESTful API,以及如何使用它构建 web 应用程序。
87 0