在ASP.NET MVC重定向当会话过期登录页面 [英] With ASP.NET MVC redirect to login page when session expires

查看:140
本文介绍了在ASP.NET MVC重定向当会话过期登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC。我想重定向当会话过期登录页面。我怎样才能做到这一点?如果我做一个AJAX调用一个方法在控制器那么如果在这种情况下我的会话过期我也想重定向到登录页面。

I am using ASP.NET MVC. I want to redirect to login page when session expires. How can I achieve this? If I am doing an AJAX call to a method in controller then if my session expires in that situation also I want to redirect to login page.

推荐答案

您可以做到这一点通过3种方式:

you could do this by 3 ways:

  1. 创建一个过滤器,你的行动,并将其应用编程OnActionExecuting一个code(动作执行之前),<一个href="http://www.asp.net/mvc/tutorials/understanding-action-filters-cs">http://www.asp.net/mvc/tutorials/understanding-action-filters-cs

创建一个基类(从控制器类继承),让你的控制器从这个继承。在这个类可以覆盖一个名为OnActionExecuting方法,像过滤器。

Create a base class (inheriting from Controller class) and make your controllers inherits from this one. In this class you could overwrite a method called OnActionExecuting, like the filter.

不使用会话进行身份验证,您可以使用窗体身份验证,并保持它的简单易用,看看这个:<一href="http://weblogs.asp.net/fredriknormen/archive/2008/02/07/asp-net-mvc-framework-using-forms-authentication.aspx">http://weblogs.asp.net/fredriknormen/archive/2008/02/07/asp-net-mvc-framework-using-forms-authentication.aspx

Don't use Session for Authentication, you can use Forms authentication and keep it simple to use, look this: http://weblogs.asp.net/fredriknormen/archive/2008/02/07/asp-net-mvc-framework-using-forms-authentication.aspx

在我看来,解决方案3比其他的要好。我希望它为你的作品! 干杯!

In my opinion, the solution 3 is better than other. I hope it works for you! Cheers!

这篇关于在ASP.NET MVC重定向当会话过期登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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