asp.Net次のアカウントでは、複数のユーザーが同時にオンラインで、繰り返しログインするコードは許可されていません.

3361 ワード

方法1:
 
  
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
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);// cache
//
}
else if (Cache[sKey].ToString() == sKey)//
{
ClientScript.RegisterStartupScript(GetType(), " ", "alert(' , ');");
return;
}
else
{
Session.Abandon();//
}
 
  
// Cache . aspx onunload . ajax hOnline Session.SessionID
window.onunload=function(){
$.ajax({
type: "POST",
   data:"sKey="+sKey;
url: "online.aspx"
});
}

online.aspx.csコード
 
  
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();
}

方法2:
 
  
//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;
}

// Session.SessionID . aspx onunload . ajax Session.SessionID
window.onunload=function(){
$.ajax({
type: "POST",
url: "online.aspx"
});
}

online.aspx.csコード
 
  
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();
}
}