2016-11-30 87 views
1

什么是配置超时页面的正确方法,当会话过期并访问授权页面时会自动重定向到页面?使用asp.net核心重定向到超时页面

I'm设置我的会话超时这样的:

services.AddSession(options => 
{ 
    options.IdleTimeout = TimeSpan.FromMinutes(120); 
}); 

回答

0

配置cookie名称:

services.AddSession(options => 
{ 
    options.CookieName = ".MyProjectName.Session"; 
    options.IdleTimeout = TimeSpan.FromMinutes(120); 
}); 

,并尝试使用以下属性:

[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
public class CheckSessionOutAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     HttpContext context = HttpContext.Current; 
     if (context.Session != null) 
     { 
      if (context.Session.IsNewSession) 
      { 
       string sessionCookie = context.Request.Headers["Cookie"]; 

       if ((sessionCookie != null) && (sessionCookie.IndexOf("MyProjectName.Session") >= 0)) 
       { 
        FormsAuthentication.SignOut(); 
        string redirectTo = "~/Account/Login"; //YOUR LOGIN PAGE HERE 
        if (!string.IsNullOrEmpty(context.Request.RawUrl)) 
        { 
         redirectTo = string.Format("~/Account/Login?ReturnUrl={0}", HttpUtility.UrlEncode(context.Request.RawUrl)); 
         filterContext.Result = new RedirectResult(redirectTo); 
         return; 
        } 

       } 
      } 
     } 

     base.OnActionExecuting(filterContext); 
    } 
} 

用法:

[CheckSessionOut] 
public ViewResult Index() 
{ 
} 
+0

嗨@Dawid,这是为asp.net核心? – Beetlejuice