我將用戶名,角色和用戶ID添加到會話中,並且會話保存在SQL Server中。如果用戶在會話尚未過期時關閉並重新打開瀏覽器,那麼如何獲取添加到會話中的先前值? 例如,會話中添加了以下值。使用保存的會話變量讀取和設置變量
SessionWrapper.currentRequest.UserName = model.UserName;
SessionWrapper.currentRequest.Role = aRole;
SessionWrapper.currentRequest.UserId = userId;
用戶關閉瀏覽器並重新打開它;該會話未過期。如何恢復會話以便這些變量保持不變?
//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" />