单点登录实例

<%
@ Page Language
=
"
C#
"
 
%>


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< script runat ="server" >

protected
void Login_Click(object sender, EventArgs e)
{
// 作为唯一标识的Key,应该是唯一的,这可根据需要自己设定规则。
// 做为测试,这里用用户名和密码的组合来做标识;也不进行其它的错误检查。

// 判断用户名和密码,省略
// 生成Key
string sKey = UserName.Text;
// 得到Cache中的给定Key的值
string sUser = Convert.ToString(Cache[sKey]);
// 检查是否存在
if (sUser == null || sUser == String.Empty)
{
// Cache中没有该Key的项目,表名用户没有登录,或者已经登录超时
// 注意下面使用的TimeSpan构造函数重载版本的方法,是进行是否登录判断的关键。
TimeSpan SessionTimeOut = new TimeSpan( 0 , 0 , System.Web.HttpContext.Current.Session.Timeout, 0 , 0 );
HttpContext.Current.Cache.Insert(sKey, sKey,
null , DateTime.MaxValue, SessionTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable,
null );
Session[
" User " ] = sKey;
// 首次登录,您可以做您想做的工作了。
Msg.Text = " <h4 style='color:red'>嗨!欢迎您访问<a href='http://dotnet.aspx.cc/'>【孟宪会之精彩世界】 " ;
Msg.Text
+= " </a>,祝您浏览愉快!:)</h4> " ;
}
else
{
// 在 Cache 中发现该用户的记录,表名已经登录过,禁止再次登录
Msg.Text = " <h4 style='color:red'>抱歉,您好像已经登录了呀:-(</h4> " ;
return ;
}
}

protected
void Page_Load(object sender, EventArgs e)
{
Time.Text
= " 自动失效时间(分钟): " + System.Web.HttpContext.Current.Session.Timeout.ToString();
}

protected
void Button1_Click(object sender, EventArgs e)
{
if (Session[ " User " ] != null )
HttpContext.Current.Cache.Remove(Session[
" User " ].ToString());
}
</ script >

< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title ></ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div align ="center" >
< h3 > 单点登录测试 </ h3 >
< p > 用户名称: < asp:TextBox ID ="UserName" runat ="server" ></ asp:TextBox ></ p >
< p > 用户密码: < asp:TextBox ID ="PassWord" runat ="server" TextMode ="Password" ></ asp:TextBox ></ p >
< p >
< asp:Button ID ="Login" runat ="server" Text =" 登 录 " OnClick ="Login_Click" ></ asp:Button >
< asp:Button ID ="Button1" runat ="server" Text ="撤销登录" onclick ="Button1_Click" />
</ p >
< p >
< asp:Label ID ="Msg" runat ="server" ></ asp:Label ></ p >
</ div >
< asp:Literal ID ="Time" runat ="server" ></ asp:Literal >
</ form >
</ body >
</ html >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值