实现思路
利用Cache的功能,我们把用户的登录信息保存在Cache中,并设置过期时间为Session失效的时间,因此,一旦Session失效,我们的Cache也过期;而Cache对所有的用户都可以访问,因此,用它保存用户信息比数据库来得方便。

        代码       string sKey = username.Text.ToString().Trim(); // 得到Cache中的给定Key的值
        string sUser = Convert.ToString(Cache[sKey]); // 检查是否存在
        if (sUser == null || sUser == String.Empty)
        {         
          TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//取得Session的过期时间
          HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);//将值放入cache己方便单点登录
        //成功登录
        }
        else if (Cache[sKey].ToString() == sKey)//如果这个账号已经登录
        {
          ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('对不起,当前用户已经登录');</script>");
          return;
        }
        else
        {
          Session.Abandon();//这段主要是为了避免不必要的错误导致不能登录
        }

 

 //关闭浏览器或窗口时清空Cache的方法.在主页面aspx文件中加入一个onunload事件.通过ajax清空hOnline中的Session.SessionID
window.οnunlοad=function(){
 $.ajax({
         type: "POST",

   data:"sKey="+sKey;
         url: "online.aspx"
 });
}

online.aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
        {

    HttpContext.Current.Cache.Remove(sKey);

        }

 //在Global.asax文件中的Session_End方法里加入

//Session过期后.清空hOnline中的Session.SessionID

    Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline[Session.SessionID] != null)
            {
                hOnline.Remove(Session.SessionID);
                Application.Lock();
                Application["Online"] = hOnline;
                Application.UnLock();
            }

 二、

 1 //sKey为登录用户名
 2 if(ApplicationOnline(username.Text.tirm())){
 3     Hashtable hOnline = new Hashtable();
 4     hOnline[Session.SessionID] = sKey;
 5     Application.Lock();
 6     Application["Online"] = hOnline;
 7     Application.UnLock();
 8 }
 9
10 public Boolean ApplicationOnline(string sKey)
11         {
12             Boolean flag = true;
13             Hashtable hOnline = (Hashtable)Application["Online"];
14             if (hOnline != null)
15             {
16                 IDictionaryEnumerator idE = hOnline.GetEnumerator();
17                 while (idE.MoveNext())
18                 {
19                     //if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID))
20                     //{
21                     if (idE.Value != null && sKey.Equals(idE.Value.ToString()))
22                     {
23                         flag = false;
24                     }
25                     break;
26                     //}
27                 }
28             }
29             return flag;
30         }
31
32 //关闭浏览器或窗口时清空Session.SessionID的方法.在主页面aspx文件中加入一个onunload事件.通过ajax清空Session.SessionID
33 window.οnunlοad=function(){
34     $.ajax({
35             type: "POST",
36                  url: "online.aspx"
37     });
38 }

 online.aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline[Session.SessionID] != null)
            {
                hOnline.Remove(Session.SessionID);
                Application.Lock();
                Application["Online"] = hOnline;
                Application.UnLock();
            }
        }


本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20101014/9279.html