如何重定向到登录页面时,会话状态超时在asp.net mvc的完成 [英] How to redirect to logon page when session State time out is completed in asp.net mvc

查看:909
本文介绍了如何重定向到登录页面时,会话状态超时在asp.net mvc的完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net应用mvc4。在我采取sessionTimeout。我有这样定义的:

I am using asp.net mvc4 application. In that i am implementing sessionTimeout. I have defined like:

              <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分钟的会议上已经完成

在我需要重定向到登录页面。但是,重定向不这样做。我怎样才能改变code,这样会重定向。
任何帮助鸭preciated

After the session of 2 mins has been completed i need to redirect to the logon page. But the redirection is not done. How can i change the code so that it will redirect. Any Help is Appreciated

推荐答案

的一种方法是,
在会话的情况下到期,每一个动作,你必须检查它的会话,如果它为null,则重定向到登录页面。

One way is that In case of Session Expire, in every action you have to check its session and if it is null then redirect to Login page.

但是,这是非常忙碌的方法
要通过这个来,你需要创建自己的 ActionFilterAttribute 这将做到这一点,你只需要在每一个动作方法中添加这个属性。

But this is very hectic method To over come this you need to create your own ActionFilterAttribute which will do this, you just need to add this attribute in every action method.

下面是它覆盖ActionFilterAttribute类。<​​/ P>

Here is the Class which overrides 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);
        }
    }

然后在行动只是添加此属性,像这样:

Then in action just add this attribute like so:

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

这会做你的工作。

这篇关于如何重定向到登录页面时,会话状态超时在asp.net mvc的完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆