利用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.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
string SigleUserLoginId dot.gifget; }
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.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public BasePage()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//
InBlock.gif        
// TODO: 在此处添加构造函数逻辑
InBlock.gif        
//
ExpandedSubBlockEnd.gif
    }

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

ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
ISingleLogin 成员#region ISingleLogin 成员
InBlock.gif
InBlock.gif    
public string SigleUserLoginId
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Session["UserId"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Session["UserId"].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
return "";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void SigleUserLogout()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Session.Abandon();
InBlock.gif        Response.Write(
"你在别处已经登陆,强制退出本次登陆!");
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

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.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SingleLoginModuler 的摘要说明
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SingleLoginModuler : System.Web.IHttpModule
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (loginedUserIdDictionary == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    loginedUserIdDictionary 
= new StringLifeValueDictionary();
ExpandedSubBlockEnd.gif                }

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

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
foreach (string key in listRemove)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (preLogoutSessionIdDictionary == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    preLogoutSessionIdDictionary 
= new StringLifeValueDictionary();
ExpandedSubBlockEnd.gif                }

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

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
foreach (string key in listRemove)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.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.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IHttpModule 成员#region IHttpModule 成员
InBlock.gif
InBlock.gif        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Init(HttpApplication context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            context.PreRequestHandlerExecute 
+= new EventHandler(context_PreRequestHandlerExecute);
InBlock.gif            context.PostRequestHandlerExecute 
+= new EventHandler(context_PostRequestHandlerExecute);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
void context_PreRequestHandlerExecute(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpApplication context 
= sender as HttpApplication;
InBlock.gif            IHttpHandler httpHandler 
= context.Context.CurrentHandler;
InBlock.gif            
if (httpHandler is ISingleLogin)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ISingleLogin sl 
= httpHandler as ISingleLogin;
InBlock.gif                
string suid = sl.SigleUserLoginId;
InBlock.gif                
if (suid != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (PreLogoutSessionIdDictionary.ContainsKey(context.Session.SessionID))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//这个用户应该强制登出
InBlock.gif
                        PreLogoutSessionIdDictionary.Remove(context.Session.SessionID);
InBlock.gif
InBlock.gif                        Page page 
= (Page)httpHandler;
InBlock.gif                        page.PreInit 
+= new EventHandler(page_PreInit);
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (!LoginedUserIdDictionary.ContainsKey(suid))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        LoginedUserIdDictionary.Add(suid, 
new LifeValue(context.Session.SessionID));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void page_PreInit(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Page page 
= sender as Page;
InBlock.gif            ISingleLogin sl 
= page as ISingleLogin;
InBlock.gif            
if (sl != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sl.SigleUserLogout();
InBlock.gif                page.Response.End();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
void context_PostRequestHandlerExecute(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//从LogineduserId 里找到和当前用户一样的用户ID的SessionId
InBlock.gif
            HttpApplication context = sender as HttpApplication;
InBlock.gif            IHttpHandler httpHandler 
= context.Context.CurrentHandler;
InBlock.gif            
if (httpHandler is ISingleLogin)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ISingleLogin sl 
= httpHandler as ISingleLogin;
InBlock.gif                
string suid = sl.SigleUserLoginId;
InBlock.gif                
if (suid != string.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (LoginedUserIdDictionary.ContainsKey(suid))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string sessionId = LoginedUserIdDictionary[suid].value;
InBlock.gif                        
if (sessionId != context.Session.SessionID)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (!PreLogoutSessionIdDictionary.ContainsKey(sessionId))
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                PreLogoutSessionIdDictionary.Add(sessionId,
new LifeValue(suid));
ExpandedSubBlockEnd.gif                            }

InBlock.gif
InBlock.gif                            LoginedUserIdDictionary.Remove(suid);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        LoginedUserIdDictionary.Add(sl.SigleUserLoginId, 
new LifeValue( context.Session.SessionID));
ExpandedSubBlockEnd.gif                    }

InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class LifeValue
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string value;
InBlock.gif        
public DateTime life;
InBlock.gif
InBlock.gif        
public LifeValue(string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

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

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xiongeee/archive/2007/03/21/683267.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值