单点登录:小技巧:在ASP.NET 中实现单点登录来源: 发布时间:星期四, 2009年2月12日 浏览:148次 评论:0
由于某些原因 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 实现思路 利用Cache ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() SingleLogin.aspx代码 <%@ Page language="c#" Codebehind="SingleLogin.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.SingleLogin" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>单点登录测试</title> <meta http-equiv="Content-Type" content="text/html; char ![]() <meta http-equiv="Author" content="孟子E章"> <meta http-equiv="WebSite" content="http://dotnet.aspx.cc/"> <style> H3 { FONT: 17px 宋体 } INPUT { FONT: 12px 宋体 } SPAN { FONT: 12px 宋体 } P { FONT: 12px 宋体 } H4 { FONT: 12px 宋体 } </style> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" 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=" 登 录 "></asp:Button></p> <p><asp:Label id="Msg" runat="server"></asp:Label></p> </div> </form> </body> </HTML> SingleLogin.aspx.cs代码 using ![]() using ![]() using ![]() using ![]() using ![]() using ![]() using ![]() using ![]() using ![]() using ![]() ![]() { /// <summary> /// SingleLogin ![]() ![]() /// 实现单点登录 /// </summary> public ![]() ![]() { protected ![]() protected ![]() protected ![]() protected ![]() private void Page_Load(object sender, ![]() { // 实际例子可访问: // http://dotnet.aspx.cc/Exam/SingleLogin.aspx } #region Web 窗体设计器生成 ![]() override protected void _disibledevent=> this.Load ![]() ![]() ![]() } #endregion private void Login_Click(object sender, ![]() { // 作为唯 ![]() ![]() ![]() ![]() ![]() ![]() ![]() // 做为测试 ![]() ![]() ![]() ![]() ![]() // 生成Key ![]() // 得到Cache中 ![]() ![]() ![]() ![]() // 检查是否存在 ![]() ![]() ![]() { // Cache中没有该Key ![]() ![]() ![]() // 注意下面使用 ![]() ![]() ![]() ![]() ![]() ![]() TimeSpan SessTimeOut = ![]() ![]() HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut, ![]() Session["User"] = sKey; // 首次登录 ![]() ![]() ![]() Msg.Text="<h4 style='color:red'>嗨!欢迎您访问<a href='http://dotnet.aspx.cc/'>【孟宪会的精彩世界】"; Msg.Text ![]() ![]() } ![]() { // 在 Cache 中发现该用户 ![]() ![]() ![]() Msg.Text="<h4 style='color:red'>抱歉 ![]() ![]() } } } } 0
相关文章读者评论发表评论 |