由於某些原因,在我們的應用中會遇到一個使用者只能在一個地方登入的情況,也就是我們通常所說的單點登入。在ASP.NET中實現單點登入其實很簡單,下面就把主要的方法和全部代碼進行分析。
實現思路
利用Cache的功能,我們把使用者的登入資訊儲存在Cache中,並設定到期時間為Session失效的時間,因此,一旦Session失效,我們的Cache也到期;而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; charset=gb2312">
<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 System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace eMeng.Exam
{
/**//// <summary>
/// SingleLogin 的摘要說明。
/// 實現單點登入
/// </summary>
public class SingleLogin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.TextBox PassWord;
protected System.Web.UI.WebControls.Label Msg;
protected System.Web.UI.WebControls.Button Login;
private void Page_Load(object sender, System.EventArgs e)
{
// 實際例子可訪問:
// http://dotnet.aspx.cc/Exam/SingleLogin.aspx
}
Web 表單設計器產生的程式碼#region Web 表單設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Login.Click += new System.EventHandler(this.Login_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Login_Click(object sender, System.EventArgs e)
{
// 作為唯一標識的Key,應該是唯一的,這可根據需要自己設定規則。
// 做為測試,這裡用使用者名稱和密碼的組合來做標識;也不進行其它的錯誤檢查。
// 產生Key
string sKey = UserName.Text + "_" + PassWord.Text;
// 得到Cache中的給定Key的值
string sUser = Convert.ToString(Cache[sKey]);
// 檢查是否存在
if (sUser == null || sUser == String.Empty)
{
// Cache中沒有該Key的項目,表名使用者沒有登入,或者已經登入逾時
// 注意下面使用的TimeSpan建構函式重載版本的方法,是進行是否登入判斷的關鍵。
TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0);
HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut,
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;
}
}
}
}