0

對不起,如果可能重複:我一直在尋找沒有成功。如何設置交叉控制器會話TimeOut以匹配ASP.NET MVC中的成員資格timeOut

從今天下午開始我就面臨一個問題:問題是我的中有一個CrossControllerSession屬性,它承載着子控制器的Session。我的MVC應用程序的每個控制器更新/讀取此屬性。

此會話根據超時時間到期(請參閱我的Global.asaxweb.config文件)。 發生什麼情況是,有時會在CrossControllerSession超時之前超時,導致所有變量在用戶仍處於連接狀態時爲空。

任何人都可以告訴我如何設置我的跨控制器會話超時爲了匹配ASP.NET MVC中的成員資格timeOut?如果是,請提前致謝。

代碼

[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); 
    } 
} 

兒童控制器

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"/> 

親切的問候!

回答

0

與配置文件不僅解決

Web.config

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

而且

<system.web> 

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

另外,我從Global.asax文件 謝謝你扔下Application_Start方法!