In my asp.net web application i want to prevent multiple user login from the same user name on different machine or the same pc by using Cache.So, in my Global.asax.as page,
i put this line…
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];
}
}
and in my page login submit button click,
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;
}
But when run my application, It is raising Session state is not available in this context error message on Application_PreRequestHandlerExecute event…
I dont know what is the issue here…So, please guide me to get out of this issue…
Or tell me another good approach instead of this one…
Session state is not available during the initial request and may be null in subsequent requests. Check for null on session state object, not just the key/value.
Side note, Your use of Response.Write here…while not invalid, is atypical. I would suggest redirection to areas.