Question

Sorry if possible duplicate: I've been looking without success.

I'm facing an issue since this afternoon: The problem is that I have a CrossControllerSession property within my BaseController that hosts the Session of Sub Controllers. Each Controller of my MVC App updates/reads this property.

This session expires according to the timeOut(see my Global.asax and web.config files). What occurs is that sometimes the CrossControllerSession times out before that the Membership times out Which results in all variables being null while the user is still connected.

Can anyone tell me How to Set my Cross Controller Session TimeOut in order to match the membership timeOut in ASP.NET MVC ? If yes, thank you in advance.

Codes

   [Authorize]
//[AllowAnonymous]
public class BaseController : Controller
{
    private static HttpSessionStateBase _mysession;
    internal protected static HttpSessionStateBase CrossControllerSession
    {
        get { return _mysession; }
        set { _mysession = value; }
    }
    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);
    }
}

Child Controller

  public class MeController : BaseController
{
        MonitoringEntitiesDataContext db = new MonitoringEntitiesDataContext();

        public ActionResult Provinces(FormsFilter filter,string partial) {
            int minId=0;
            //Session["UserName"]=User.Identity.Name;
           // var session = BaseController.CrossControllerSession;
        try
        {
            string minIdString = BaseController.CrossControllerSession["CodeModule"].ToString();
            int.TryParse(minIdString, out minId);
        }
        catch (Exception)
        {

        }
        var provinces = db.V_STAT_ME_PROVINCEs
            .Where(e => e.CodeMinistere == minId);
        ViewBag.Session = BaseController.CrossControllerSession;
        return View(provinces);
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        BaseController.CrossControllerSession = Session;
        base.OnActionExecuting(filterContext);
    }
}

Global.asax

 protected void Session_End(Object sender, EventArgs e)
    {
        Session.Clear();
        Session.Abandon();
        Session.Clear();//lol
    }
    protected void Session_Start(Object sender, EventArgs e)
    {
        int timeOut = 1;
        try
        {
            int.TryParse(ConfigurationManager.AppSettings["SessionTimeOut"].ToString(), out timeOut);
        }
        catch
        { //error logging here
        }
        Session.Timeout = timeOut;
    }

Web.Config

<add key="SessionTimeOut" value="15"/>

Kind Regards!

Was it helpful?

Solution

Solved only with the config file

Web.config

  <authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="2" slidingExpiration="true" name=".ASPXFORMSAUTH" />
 ...
 </authentication>

And

 <system.web>

<sessionState
  mode="InProc"
  cookieless="false"
  timeout="2" /> 
...
</system.web>

In addition, I have dropped the Application_Start method from the Global.asax file Thank you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top