實(shí)現(xiàn)單點(diǎn)登錄(附全碼)_AX
①概念:單點(diǎn)登錄就是同一時(shí)刻某一用戶只能在一個(gè)地點(diǎn)登錄系統(tǒng).
②實(shí)現(xiàn)
通過(guò)Cache來(lái)保證用戶只能登錄一次.因?yàn)镃ache是Application level的.
不過(guò)猜想當(dāng)用戶同時(shí)在線量很大時(shí)會(huì)出現(xiàn)問(wèn)題,10萬(wàn)的用戶Cache可不是說(shuō)著玩的,不過(guò)一般除了網(wǎng)游好像很難達(dá)到這個(gè)數(shù)量級(jí).
③本文源自【孟憲會(huì)之精彩世界】,略做修改,特此感謝!
附:源碼
前臺(tái):
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>單點(diǎn)登錄測(cè)試</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>單點(diǎn)登錄測(cè)試</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

后臺(tái):
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.Exam13


{14

/**//// <summary>15
/// SingleLogin 的摘要說(shuō)明。16
/// 實(shí)現(xiàn)單點(diǎn)登錄17
/// </summary>18
public class SingleLogin : System.Web.UI.Page19

{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
// 實(shí)際例子可訪問(wèn):37
// http://dotnet./Exam/SingleLogin.aspx38
}39

40

Web 窗體設(shè)計(jì)器生成的代碼#region Web 窗體設(shè)計(jì)器生成的代碼41
override protected void OnInit(EventArgs e)42

{43
InitializeComponent();44
base.OnInit(e);45
}46

47

/**//// <summary>48
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改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
#endregion58

59
private void Login_Click(object sender, System.EventArgs e)60

{61
// //Session不能實(shí)現(xiàn)單點(diǎ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
// else70
// {71
// Response.Write("已經(jīng)登錄!");72
// }73

74

75
// 作為唯一標(biāo)識(shí)的Key,應(yīng)該是唯一的,這可根據(jù)需要自己設(shè)定規(guī)則。76
// 做為測(cè)試,這里用用戶名和密碼的組合來(lái)做標(biāo)識(shí);也不進(jìn)行其它的錯(cuò)誤檢查。77

78
// 生成Key79
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中沒(méi)有該Key的項(xiàng)目,表名用戶沒(méi)有登錄,或者已經(jīng)登錄超時(shí)86
// 注意下面使用的TimeSpan構(gòu)造函數(shù)重載版本的方法,是進(jìn)行是否登錄判斷的關(guān)鍵。???好象不是哦!87
//SessTimeOut是設(shè)定的一個(gè)TimeSpan,該處設(shè)定為Session登錄超時(shí)的時(shí)間,我本機(jī)為20分鐘,88
//見(jiàn)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‘>嗨!歡迎您訪問(wèn)<a href=‘http://dotnet./‘>【孟憲會(huì)之精彩世界】";94
Msg.Text+="</a>,祝您瀏覽愉快?。海?lt;/h4>";95
Msg.Text+="<h4 style=‘color:red‘>AX也祝您愉快!</h4>";96
}97
else98

{99
// 在 Cache 中發(fā)現(xiàn)該用戶的記錄,表名已經(jīng)登錄過(guò),禁止再次登錄100
Msg.Text="<h4 style=‘color:red‘>抱歉,您好像已經(jīng)登錄了呀:-(</h4>";101
}102
}103
}104
}105

posted on 2007-01-11 11:03 斧頭幫少幫主 閱讀(161) 評(píng)論(5) 編輯 收藏 引用 網(wǎng)摘 所屬分類: 經(jīng)驗(yàn)總結(jié)





