防止论坛用户重复登录的方法 .
本例完成的功能就是防止用戶重復(fù)登錄!若用戶已經(jīng)登錄,則當(dāng)其再次登錄時,彈出提示框后返回!
實現(xiàn)思路:用戶登錄成功后,將用戶登錄信息存放到Hashtable類型的Application["Online"]里面,其鍵值為SessionID,其Value值為用戶ID;當(dāng)用戶注銷時,調(diào)用Session.Abandon;在Global.asax里面的SessionEnd事件中,將用戶ID從Hashtable中刪除;在用戶訪問頁面時,察看Hashtable中是否有對應(yīng)的用戶ID如果沒有則判斷用戶不在線(用戶不在線的原因可能是按了注銷按鈕、網(wǎng)頁超時等)
1、公用類中判斷用戶是否在線的函數(shù)(供用戶調(diào)用)
/// <summary>
/// 判斷用戶strUserID是否包含在Hashtable h中
/// </summary>
/// <param name="strUserID"></param>
/// <param name="h"></param>
/// <returns></returns>
public static bool AmIOnline(string strUserID,Hashtable h)
{
if(strUserID == null)
return false;
//繼續(xù)判斷是否該用戶已經(jīng)登陸
if(h == null)
return false;
//判斷哈希表中是否有該用戶
IDictionaryEnumerator e1 = h.GetEnumerator();
bool flag = false;
while(e1.MoveNext())
{
if(e1.Value.ToString().CompareTo(strUserID) == 0)
{
flag = true;
break;
}
}
return flag;
}
2、用戶登錄事件處理:
private void btnlogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{ User為自定義的類,其中包含Login方法
User CurUser = new User();
CurUser.UserID = this.username.Text.Trim();
if(MyUtility.AmIOnline(CurUser.UserID,(Hashtable)Application["Online"]))
{
JScript.Alert("您所使用的登錄ID已經(jīng)在線了!您不能重復(fù)登錄!");
return;
}
CurUser.LoginPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(this.password.Text.Trim(),"SHA1");
int ii = CurUser.Login();
StringBuilder sbPmt = new StringBuilder();
switch(ii)
{
case 0: //如果登錄成功,則將UserID加入Application["Online"]中
Hashtable h = (Hashtable)Application["Online"];
if(h == null)
h = new Hashtable();
h[Session.SessionID] = CurUser.UserID;
Application["Online"] = h;
Session["UserID"] = CurUser.UserID;
Session["UserNM"] = CurUser.UserNM;
Session["RoleMap"] = CurUser.RoleMap;
Session["LoginPsw"] = CurUser.LoginPsw;
Session["LoginTime"] = DateTime.Now;
Response.Redirect("ChooseRole.aspx");
break;
case -1:
JScript.Alert("用戶名錯誤!");
break;
case -2:
JScript.Alert("密碼錯誤!");
break;
default:
sbPmt.Append("登錄過程中發(fā)生未知錯誤!");
JScript.Alert(sbPmt.ToString());
break;
}
return;
}
3、在Global.asax中的Session_End事件:
protected void Session_End(Object sender, EventArgs e)
{
Hashtable h=(Hashtable)Application["Online"];
if(h[Session.SessionID]!=null)
h.Remove(Session.SessionID);
Application["Online"]=h;
}
4、在每一個頁面需要刷新的地方,調(diào)用如下代碼:
try
{
if(!common.MyUtility.AmIOnline(Session["UserID"].ToString(),(Hashtable)Application["OnLine"]))
{
//用戶沒有在線 ,轉(zhuǎn)到登錄界面
Response.Write("<script>parent.document.location.href='Login.aspx';</script>"); 有框架時用
//Response.Redirect("login.aspx"); 無框架時用
return;
}
}
catch
{
//會話過期 ,轉(zhuǎn)到登錄界面
Response.Write("<script>parent.document.location.href='Login.aspx';</script>"); 有框架時所用
//Response.Redirect("login.aspx"); 無框架時用
return;
}
深入思考:
由本例的解決方法可以加以延伸,比如,在存儲UserID的時候,將UserID+客戶端IP地址一起存進去,則在將相應(yīng)信息取出來分析的時候,可以做到:當(dāng)用戶在不同的計算機上先后登錄的時候,則允許最近一次的登錄,而將之前的登錄刪除!等等等等?
轉(zhuǎn)載于:https://www.cnblogs.com/leotan/archive/2011/12/26/2301501.html
總結(jié)
以上是生活随笔為你收集整理的防止论坛用户重复登录的方法 .的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android菜单详解三:上下文菜单
- 下一篇: 简单-三层-存储过程-增删改四