实现单点登录

 1if(this.user.Text.Trim()!="" && this.pwd.Text.Trim()!="")
 2            {
 3                // 生成Key
 4                string sKey = user.Text + "_" + pwd.Text;
 5                // 得到Cache中的给定Key的值
 6                string sUser = Convert.ToString(Cache[sKey]);
 7                // 检查是否存在
 8                if (sUser == null || sUser == String.Empty)
 9                {
10                    // Cache中没有该Key的项目,表名用户没有登录,或者已经登录超时
11                    // 注意下面使用的TimeSpan构造函数重载版本的方法,是进行是否登录判断的关键。                
12                    TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
13                    HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut,System.Web.Caching.CacheItemPriority.NotRemovable,null);
14                    Session["User"= sKey;
15                    //首次登录,您可以做您想做的工作了。
16                    Msg.Text="<h4 style='color:red'>嗨!欢迎您访问<a href='http://dotnet.aspx.cc/'>;【孟宪会之精彩世界】";
17                    Msg.Text += "</a>,祝您浏览愉快!:)</h4>";
18                }

19                else
20                {
21                    // 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录
22                    Msg.Text="<h4 style='color:red'>抱歉,您好像已经登录了呀:-(</h4>";
23                    return;
24                }
原文地址:https://www.cnblogs.com/crith/p/695667.html