在我的asp.net web應用程序,我想,以防止不同機器或在同一臺PC上相同的用戶名多個用戶登錄在我的Global.asax.as頁面使用Cache.So,如何防止多個用戶使用緩存在asp.net中使用相同的用戶ID登錄?
我把這個行...
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
if (Session["user"] != null) // e.g. this is after an initial logon
{
string sKey = (string)Session["user"];
string sUser = (string)HttpContext.Current.Cache[sKey];
}
}
,並在我的網頁登錄提交按鈕點擊,
string userName=uName.Text;
string passWord=pwd.Text;
string sKey = uName.Text;
string sUser = Convert.ToString(Cache[sKey]);
if (sUser == null || sUser == String.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);
Session["user"] = TextBox1.Text.Trim();
if (userName.Equals(db_userName) && passWord.Equals(db_password))
{
Response.Write("Welcome " + userName);
}
else
{
Response.Write("Invalid Login");
}
}
else
{
Response.Write("<Marquee><h1><font color=red>ILLEGAL LOGIN ATTEMPT!!!</font></h1></marquee>");
return;
}
但運行我的應用程序時,它是提高會話狀態在Application_PreRequestHandlerExecute事件這種情況下錯誤信息不可用。 ..
我不知道什麼是這裏的問題......所以,請指引我走出這個問題...
或者告訴我另一個好辦法,而不是這一個...
:感謝您的寶貴suggestion.Could請你告訴我「如何檢查會話狀態對象爲空」? – Saravanan
我在帖子中爲您提供了一個示例。 – Hardrada
非常感謝...剛纔我登錄並用註銷關閉瀏覽器。所以,下次嘗試...但它阻止了我登錄...用戶會話緩存持續多久... – Saravanan