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

推荐订阅源

H
Heimdal Security Blog
P
Privacy International News Feed
S
Schneier on Security
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
Spread Privacy
Spread Privacy
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Scott Helme
Scott Helme
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
aimingoo的专栏
aimingoo的专栏
Simon Willison's Weblog
Simon Willison's Weblog
S
Securelist
Help Net Security
Help Net Security
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Archives - TechRepublic
Security Archives - TechRepublic
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
V
V2EX
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
N
News | PayPal Newsroom
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
P
Palo Alto Networks Blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
博客园 - Franky
I
InfoQ
D
DataBreaches.Net
爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - dyq

ASP.NET架构分析 ASP.NET 页面对象模型 基于aspnet Forms身份验证基本原理 - dyq - 博客园 实现简单的 Forms 身份验证 ASP.NET 中处理页面“回退”的方法 建立一个使用.Net 2.0 MemberShip功能的标准例程(二)——配置篇 GridView 72般绝技 SQL Server 连接字符串和身份验证 .NET程序中常用的代码 - dyq - 博客园 分析ASP.NET服务器控件开发-控件生命周期 收藏网站制作常用经典ajax.prototype.javascript实例打包下载 asp.net中<%%>形式的用法(原创) .NET模板引擎 EFPlatform.CodeGenerator (代码生成器 静态页生成 生成HTML 模板) 深入浅出之正则表达式(一) 正则表达式学习 常用的正则表达式 ASP.NET URL Rewrite. URL重写 ASP.NET 生成静态页 .NET生成静态页面并分页
利用HttpModuler实现WEB程序同一时间只让一个用户实例登陆
dyq · 2008-02-16 · via 博客园 - dyq

转自: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";

        
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: 在此处添加构造函数逻辑
            
//
        }


        
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();

    }

}

     如此,只在在你自己的BasePage中改动相关的代码(只两三行)就可以实现功能了。