①概念: 單點登錄就是同一時刻某一用戶只能在一個地點登錄系統(tǒng). ②實現(xiàn) 通過Cache來保證用戶只能登錄一次.因為Cache是Application level的. 不過猜想當(dāng)用戶同時在線量很大時會出現(xiàn)問題,10萬的用戶Cache可不是說著玩的,不過一般除了網(wǎng)游好像很難達到這個數(shù)量級. ③本文源自【孟憲會之精彩世界】,略做修改,特此感謝!
附:源碼 前臺:
  1 <% @ Page language="c#" Codebehind="SingleLogin.aspx.cs" AutoEventWireup="false" 2 Inherits="eMeng.Exam.SingleLogin" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 4 <HTML> 5 <HEAD> 6 <title>單點登錄測試</title> 7 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 8 <meta http-equiv="Author" content="孟子E章"> 9 <meta http-equiv="WebSite" content="http://dotnet."> 10 <style> H3 { }{ 11 FONT: 17px 宋體 12 } 13 INPUT { }{ 14 FONT: 12px 宋體 15 } 16 SPAN { }{ 17 FONT: 12px 宋體 18 } 19 P { }{ 20 FONT: 12px 宋體 21 } 22 H4 { }{ 23 FONT: 12px 宋體 24 } 25 </style> 26 </HEAD> 27 <body ms_positioning="GridLayout"> 28 <form id="Form1" method="post" runat="server"> 29 <div align="center"> 30 <h3>單點登錄測試</h3> 31 <p>用戶名稱:<asp:TextBox id="UserName" runat="server"></asp:TextBox></p> 32 <p>用戶密碼:<asp:TextBox id="PassWord" runat="server" TextMode="Password"></asp:TextBox></p> 33 <p><asp:Button id="Login" runat="server" Text=" 登 錄 "></asp:Button></p> 34 <p><asp:Label id="Msg" runat="server"></asp:Label></p> 35 </div> 36 </form> 37 </body> 38 </HTML> 39
后臺:
  1 using System; 2 using System.Collections; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Web; 7 using System.Web.SessionState; 8 using System.Web.UI; 9 using System.Web.UI.WebControls; 10 using System.Web.UI.HtmlControls; 11 12 namespace eMeng.Exam 13  { 14 /**//// <summary> 15 /// SingleLogin 的摘要說明。 16 /// 實現(xiàn)單點登錄 17 /// </summary> 18 public class SingleLogin : System.Web.UI.Page 19 { 20 protected System.Web.UI.WebControls.TextBox UserName; 21 protected System.Web.UI.WebControls.TextBox PassWord; 22 protected System.Web.UI.WebControls.Label Msg; 23 protected System.Web.UI.WebControls.Button Login; 24 25 private void Page_Load(object sender, System.EventArgs e) 26 { 27 //字符串倒置 28 string s="hello_XA"; 29 string ss=""; 30 for(int i=s.Length-1;i>=0;i--) 31 { 32 ss+=s[i]; 33 } 34 Response.Write(ss); 35 Response.Write("<br>"+DateTime.MaxValue+"||"+System.Web.HttpContext.Current.Session.Timeout); 36 // 實際例子可訪問: 37 // http://dotnet./Exam/SingleLogin.aspx 38 } 39 40 Web 窗體設(shè)計器生成的代碼#region Web 窗體設(shè)計器生成的代碼 41 override protected void OnInit(EventArgs e) 42 { 43 InitializeComponent(); 44 base.OnInit(e); 45 } 46 47 /**//// <summary> 48 /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改 49 /// 此方法的內(nèi)容。 50 /// </summary> 51 private void InitializeComponent() 52 { 53 this.Login.Click += new System.EventHandler(this.Login_Click); 54 this.Load += new System.EventHandler(this.Page_Load); 55 56 } 57 #endregion 58 59 private void Login_Click(object sender, System.EventArgs e) 60 { 61 // //Session不能實現(xiàn)單點登錄 62 // string key=UserName.Text + "_" + PassWord.Text; 63 // string k=Convert.ToString(Session["user"]); 64 // if(k==""||k==null) 65 // { 66 // Session["user"]=key; 67 // Response.Write("首次登錄!"); 68 // } 69 // else 70 // { 71 // Response.Write("已經(jīng)登錄!"); 72 // } 73 74 75 // 作為唯一標識的Key,應(yīng)該是唯一的,這可根據(jù)需要自己設(shè)定規(guī)則。 76 // 做為測試,這里用用戶名和密碼的組合來做標識;也不進行其它的錯誤檢查。 77 78 // 生成Key 79 string sKey = UserName.Text + "_" + PassWord.Text; 80 // 得到Cache中的給定Key的值 81 string sUser = Convert.ToString(Cache[sKey]); 82 // 檢查是否存在 83 if (sUser == null || sUser == String.Empty) 84 { 85 // Cache中沒有該Key的項目,表名用戶沒有登錄,或者已經(jīng)登錄超時 86 // 注意下面使用的TimeSpan構(gòu)造函數(shù)重載版本的方法,是進行是否登錄判斷的關(guān)鍵。???好象不是哦! 87 //SessTimeOut是設(shè)定的一個TimeSpan,該處設(shè)定為Session登錄超時的時間,我本機為20分鐘, 88 //見Page_Load的最后一行輸出 89 TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0); 90 HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut, 91 System.Web.Caching.CacheItemPriority.NotRemovable,null); 92 // 首次登錄,您可以做您想做的工作了。 93 Msg.Text="<h4 style=‘color:red‘>嗨!歡迎您訪問<a href=‘http://dotnet./‘>【孟憲會之精彩世界】"; 94 Msg.Text+="</a>,祝您瀏覽愉快?。海?lt;/h4>"; 95 Msg.Text+="<h4 style=‘color:red‘>AX也祝您愉快!</h4>"; 96 } 97 else 98 { 99 // 在 Cache 中發(fā)現(xiàn)該用戶的記錄,表名已經(jīng)登錄過,禁止再次登錄 100 Msg.Text="<h4 style=‘color:red‘>抱歉,您好像已經(jīng)登錄了呀:-(</h4>"; 101 } 102 } 103 } 104 } 105
|