The session gets loaded during Application_AcquireRequestState. Your safe bet is to build Application_PreRequestHandlerExecute
and
access it there.
Update: Not every request has a session state. You need to also check for null: if
(System.Web.HttpContext.Current.Session != null)
.
var session = HttpContext.Current.Session;
if(session != null) {
/* ... do stuff ... */
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
/* ... */
}
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
if (HttpContext.Current.Session != null && HttpContext.Current.Session["isLogged"] != null && (bool)HttpContext.Current.Session["isLogged"])
{
HttpContext.Current.User = (LoginModel)HttpContext.Current.Session["LoginModel"];
}
}
}
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms