I did not have a Session_start and Session_end method in my global.asax file. I added one, and the issue went away.
Reading and Setting Variables using the Saved Session Variables
-
23-06-2023 - |
سؤال
I added the username, role, and userID to a session, and the session is save in SQL Server. If the user were to close and reopen the browser while the session is not yet expired, how do I obtain the previous values that were added to the session? For example, the following values were added to the session.
SessionWrapper.currentRequest.UserName = model.UserName;
SessionWrapper.currentRequest.Role = aRole;
SessionWrapper.currentRequest.UserId = userId;
The user closes the browser and reopens it; the session is not expired. How do I restore the session so that these variables remains intact?
//This is the object that is wrapped in the session
public class CurrentRequest
{
public int UserId { get; set; }
public string Role { get; set; }
}
//The session Wrapper
public class SessionWrapper
{
public static CurrentRequest currentRequest
{
get
{
if (HttpContext.Current.Session["request"] != null)
{
return (CurrentRequest)HttpContext.Current.Session["request"];
}
else
{
return null;
}
}
set
{
HttpContext.Current.Session["request"] = value;
}
}
}
// I am saving the following values in the session
SessionWrapper.currentRequest.UserName = model.UserName;
SessionWrapper.currentRequest.Role = aRole;
SessionWrapper.currentRequest.UserId = userId;
The sessionState mode is SQLServer, and I can see data added to the both ASPSTATE tables. here is how I set up the session state in the config file
<sessionState mode="SQLServer" allowCustomSqlDatabase="true" timeout="60"
cookieless="false" sqlConnectionString="Data Source=(****);Initial Catalog=data1;User
ID=****;Password=****;Integrated Security=True" />
المحلول