在web開發時,有的系統要求同一個用戶在同一時間只能登錄一次,也就是如果一個用戶已經登錄了,在退出之前如果再次登錄的話需要報錯。
常見的處理方法是,在用戶登錄時,判斷此用戶是否已經在Application中存在,如果存在就報錯,不存在的話就加到Application中(Application是所有Session共有的,整個web應用程序唯一的一個對象):
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
string strUserId = txtUser.Text;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
if (list == null)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524235.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
list = new ArrayList();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524355.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
for (int i = 0; i < list.Count; i++)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524235.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
if (strUserId == (list[i] as string))
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524317.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
//已經登錄了,提示錯誤信息
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
lblError.Text = "此用戶已經登錄";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
return;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524496.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524355.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
list.Add(strUserId);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
Application.Add("GLOBAL_USER_LIST", list);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
當然這裡使用Cache等保存也可以。
接下來就是要在用戶退出的時候將此用戶從Application中去除,我們可以在Global.asax的Session_End事件中處理:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
void Session_End(object sender, EventArgs e)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524235.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
// 在會話結束時運行的代碼。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
// InProc 時,才會引發 Session_End 事件。如果會話模式設置為 StateServer
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
// 或 SQLServer,則不會引發該事件。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
string strUserId = Session["SESSION_USER"] as string;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
ArrayList list = Application.Get("GLOBAL_USER_LIST") as ArrayList;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
if (strUserId != null && list != null)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524317.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
list.Remove(strUserId);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
Application.Add("GLOBAL_USER_LIST", list);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524496.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524355.gif)
}
這些都沒有問題,有問題的就是當用戶直接點浏覽器右上角的關閉按鈕時就有問題了。因為直接關閉的話,並不會立即觸發Session過期事件,也就是關閉浏覽器後再來登錄就登不進去了。
這裡有兩種處理方式:
1、使用Javascript方式 在每一個頁面中加入一段Javascript代碼:
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
function window.onbeforeunload()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524235.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524317.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524450.gif)
if (event.clientX>document.body.clientWidth && event.clIEntY<0||event.altKey)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
window.open("logout.ASPx");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524496.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524355.gif)
}
由於onbeforeunload方法在浏覽器關閉、刷新、頁面調轉等情況下都會被執行,所以需要判斷是點擊了關閉按鈕或是按下Alt+F4時才執行真正的關閉操作。
然後在logout.aspx的Page_Load中寫和Session_End相同的方法,同時在logout.ASPx中加入事件:onload="Javascript:window.close()"
但是這樣還是有問題,Javascript在不同的浏覽器中可能有不同的行為,還有就是當通過文件->關閉時沒有判斷到。
2、使用XMLhttp方法(這種方法測試下來沒有問題)
在每個頁面中加入如下的Javascript(這些Javascript也可以寫在共通裡,每個頁面引入就可以了)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
var x=0;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
function myRefresh()
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524235.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
var httpRequest = new ActiveXObject("microsoft.XMLhttp");
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
httpRequest.open("GET", "test.ASPx", false);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
httpRequest.send(null);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
x++;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
if(x<60) //60次,也就是Session真正的過期時間是30分鐘
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524317.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524343.gif)
{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524375.gif)
setTimeout("myRefresh()",30*1000); //30秒
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524496.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524355.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011310524214.gif)
myRefresh();
在web.config中設置
<sessionState mode="InProc" timeout="1"></sessionState>
test.ASPx頁面就是一個空頁面,只不過需要在Page_Load中加入:
Response.Expires = -1;
保證不使用緩存,每次都能調用到這個頁面。
原理就是:設置Session的過期時間是一分鐘,然後在每個頁面上定時每30秒連接一次測試頁面,保持Session有效,總共連60次,也就是30分鐘。如果30分鐘後用戶還沒有操作,Session就會過期。當然,如果用戶直接關閉浏覽器,那麼一分鐘後Session也會過期。這樣就可以滿足要求了。