在ASP.NET 中實現單點登錄
2024-07-10 12:56:22
供稿:網友
在asp.net 中實現單點登錄
由于某些原因,在我們的應用中會遇到一個用戶只能在一個地方登錄的情況,也就是我們通常所說的單點登錄。在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
}
#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 >【孟憲會之精彩世界】";
msg.text += "</a>,祝您瀏覽愉快!:)</h4>";
}
else
{
// 在 cache 中發現該用戶的記錄,表名已經登錄過,禁止再次登錄
msg.text="<h4 style='color:red'>抱歉,您好像已經登錄了呀:-(</h4>";
return;
}
}
}
}