欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net下一個(gè)賬號(hào)不允許多個(gè)用戶同時(shí)在線,重復(fù)登陸的代碼

 更新時(shí)間:2010年10月14日 00:28:38   作者:  
asp.net下一個(gè)賬號(hào)不允許多個(gè)用戶同時(shí)在線,重復(fù)登陸的代碼,需要的朋友可以參考下。

方法一:

復(fù)制代碼 代碼如下:

string sKey = username.Text.ToString().Trim(); // 得到Cache中的給定Key的值
string sUser = Convert.ToString(Cache[sKey]); // 檢查是否存在
if (sUser == null || sUser == String.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//取得Session的過期時(shí)間
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);//將值放入cache己方便單點(diǎn)登錄
//成功登錄
}
else if (Cache[sKey].ToString() == sKey)//如果這個(gè)賬號(hào)已經(jīng)登錄
{
ClientScript.RegisterStartupScript(GetType(), "提示", "<script>alert('對(duì)不起,當(dāng)前用戶已經(jīng)登錄');</script>");
return;
}
else
{
Session.Abandon();//這段主要是為了避免不必要的錯(cuò)誤導(dǎo)致不能登錄
}

復(fù)制代碼 代碼如下:

//關(guān)閉瀏覽器或窗口時(shí)清空Cache的方法.在主頁(yè)面aspx文件中加入一個(gè)onunload事件.通過ajax清空hOnline中的Session.SessionID
window.onunload=function(){
$.ajax({
type: "POST",
   data:"sKey="+sKey;
url: "online.aspx"
});
}

online.aspx.cs代碼
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
    HttpContext.Current.Cache.Remove(sKey);
}
//在Global.asax文件中的Session_End方法里加入
//Session過期后.清空hOnline中的Session.SessionID
    Hashtable hOnline = (Hashtable)Application["Online"];
if (hOnline[Session.SessionID] != null)
{
hOnline.Remove(Session.SessionID);
Application.Lock();
Application["Online"] = hOnline;
Application.UnLock();
}

方法二:
復(fù)制代碼 代碼如下:

//sKey為登錄用戶名
if(ApplicationOnline(username.Text.tirm())){
Hashtable hOnline = new Hashtable();
hOnline[Session.SessionID] = sKey;
Application.Lock();
Application["Online"] = hOnline;
Application.UnLock();
}

public Boolean ApplicationOnline(string sKey)
{
Boolean flag = true;
Hashtable hOnline = (Hashtable)Application["Online"];
if (hOnline != null)
{
IDictionaryEnumerator idE = hOnline.GetEnumerator();
while (idE.MoveNext())
{
//if (idE.Key != null && idE.Key.ToString().Equals(Session.SessionID))
//{
if (idE.Value != null && sKey.Equals(idE.Value.ToString()))
{
flag = false;
}
break;
//}
}
}
return flag;
}

//關(guān)閉瀏覽器或窗口時(shí)清空Session.SessionID的方法.在主頁(yè)面aspx文件中加入一個(gè)onunload事件.通過ajax清空Session.SessionID
window.onunload=function(){
$.ajax({
type: "POST",
url: "online.aspx"
});
}

online.aspx.cs代碼
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
Hashtable hOnline = (Hashtable)Application["Online"];
if (hOnline[Session.SessionID] != null)
{
hOnline.Remove(Session.SessionID);
Application.Lock();
Application["Online"] = hOnline;
Application.UnLock();
}
}

相關(guān)文章

最新評(píng)論