实现单点登录
1
if(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/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)