国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.net防止論壇用戶重復登錄的方法

2024-07-10 13:05:51
字體:
來源:轉載
供稿:網友

本例完成的功能就是防止用戶重復登錄!若用戶已經

登錄,則當其再次登錄時,彈出提示框后返回!

實現思路:用戶登錄成功后,將用戶登錄信息存放到

hashtable類型的application["online"]里面,其鍵

值為sessionid,其value值為用戶id;當用戶注銷時

,調用session.abandon;在global.asax里面的

sessionend事件中,將用戶id從hashtable中刪除;在

用戶訪問頁面時,察看hashtable中是否有對應的用戶

id如果沒有則判斷用戶不在線(用戶不在線的原因可

能是按了注銷按鈕、網頁超時等)

1、公用類中判斷用戶是否在線的函數(供用戶調用)
/// <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;

//繼續判斷是否該用戶已經登陸
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已經在線了!您不

能重復登錄!");
return;
}

curuser.loginpsw =

formsauthentication.hashpasswordforstoringincon

figfile(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("登錄過程中發生未知錯誤!");
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、在每一個頁面需要刷新的地方,調用如下代碼:
try
{
if(!common.myutility.amionline(session

["userid"].tostring(),(hashtable)application

["online"]))
{
//用戶沒有在線 ,轉到登錄界面
response.write

("<script>parent.document.location.href='login.

aspx';</script>"); ////有框架時用
//response.redirect("login.aspx"); ////無框架時


return;
}
}
catch
{
//會話過期 ,轉到登錄界面
response.write

("<script>parent.document.location.href='login.

aspx';</script>"); ////有框架時所用
//response.redirect("login.aspx"); ////無框架時


return;
}

深入思考:
由本例的解決方法可以加以延伸,比如,在存儲

userid的時候,將userid+客戶端ip地址一起存進去,

則在將相應信息取出來分析的時候,可以做到:當用

戶在不同的計算機上先后登錄的時候,則允許最近一

次的登錄,而將之前的登錄刪除!等等等等

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正阳县| 霍林郭勒市| 博爱县| 定西市| 东阿县| 嘉荫县| 河源市| 镇江市| 江山市| 丘北县| 宁南县| 达日县| 滦平县| 巴林右旗| 交城县| 宁河县| 永州市| 巴彦淖尔市| 新竹市| 五指山市| 靖远县| 南投市| 苏尼特左旗| 远安县| 崇左市| 建始县| 宣威市| 乐至县| 德钦县| 岑巩县| 东平县| 镇宁| 上虞市| 乌恰县| 宁德市| 阿荣旗| 电白县| 中山市| 海淀区| 沿河| 仁寿县|