加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

当会话状态超时在asp.net mvc中完成时,如何重定向到登录页面

发布时间:2020-12-16 00:42:14 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP.NET MVC4应用程序,我正在实现的sessionTimeout像: configuration system.web sessionState timeout="2"/sessionState /system.web/configuration 并在验证中: configuration system.web authentication mode="Forms" forms loginUrl="~/Accou
我有一个ASP.NET MVC4应用程序,我正在实现的sessionTimeout像:
<configuration>
  <system.web>
    <sessionState timeout="2"></sessionState>
  </system.web>
</configuration>

并在验证中:

<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="1" />
    </authentication>
  </system.web>
</configuration>

会话已过期(2分钟)后,我需要重定向到登录页面,但重定向不会发生。

如何更改代码以便重定向?

解决方法

一种方法就是这样
在会话过期的情况下,在每个操作中,您必须检查其会话,如果它为空,则重定向到登录页面。

但这是非常忙碌的方法
为了完成这一切,您需要创建自己的ActionFilterAttribute,这将执行此操作,您只需要在每个操作方法中添加此属性。

这是覆盖ActionFilterAttribute的类。

public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;

            // check if session is supported
            CurrentCustomer objCurrentCustomer = new CurrentCustomer();
            objCurrentCustomer = ((CurrentCustomer)SessionStore.GetSessionValue(SessionStore.Customer));
            if (objCurrentCustomer == null)
            {
                // check if a new session id was generated
                filterContext.Result = new RedirectResult("~/Users/Login");
                return;
            }

            base.OnActionExecuting(filterContext);
        }
    }

然后在操作中只需添加如下属性:

[SessionExpire]
public ActionResult Index()
{
     return Index();
}

这会让你工作。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读