使用ASP.NET MVC会话到期时重定向到登录页面

时间:2011-08-17 10:44:18

标签: ajax asp.net-mvc

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

4 个答案:

答案 0 :(得分:14)

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

  1. 为您的操作创建过滤器并应用它在OnActionExecuting中编写代码(在执行操作之前),http://www.asp.net/mvc/tutorials/understanding-action-filters-cs

  2. 创建一个基类(继承自Controller类)并使控制器继承自此类。在这个类中,您可以覆盖一个名为OnActionExecuting的方法,如过滤器。

  3. 不要使用Session进行身份验证,您可以使用Forms身份验证并使其易于使用,请查看:http://weblogs.asp.net/fredriknormen/archive/2008/02/07/asp-net-mvc-framework-using-forms-authentication.aspx

  4. 在我看来,解决方案3比其他解决方案更好。我希望它适合你!

答案 1 :(得分:7)

因为可以复制Forms-Authentication的security-cookie使用它来模拟注册用户我使用以下属性将身份验证绑定到当前会话生存期。

要使属性工作,您必须在登录时设置session [“user”] = MyUser并在注销时调用session.abandom()。 我不知道重定向是否适用于ajax调用 - 这是你必须尝试的东西。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class CheckUserSessionAttribute : ActionFilterAttribute
{

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpSessionStateBase session = filterContext.HttpContext.Session;
        var user = session["User"];

        if (((user == null) && (!session.IsNewSession)) || (session.IsNewSession))
        {
            //send them off to the login page
            var url = new UrlHelper(filterContext.RequestContext);
            var loginUrl = url.Content("~/Account/LogOff");
            session.RemoveAll();
            session.Clear();
            session.Abandon();
            filterContext.HttpContext.Response.Redirect(loginUrl, true);
        }
    }
}

答案 2 :(得分:4)

这个答案很大程度上基于Michaels,除非它有效; - )

我将其更改为接受委托以检查会话是否已结束,以便它可以在不同的应用程序中工作,这可能有不同的方法来确定这一点,并且登录页面可能在其他应用程序中有所不同。在Global.asax.cs Application_Start()中,我的应用程序中的代码是

CheckUserSessionAttribute.CheckSessionAlive = session => (session.GetUser() != null);
CheckUserSessionAttribute.LoginUrl = "~/Account/Login";

然后属性类如下

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class CheckUserSessionAttribute : ActionFilterAttribute
    {
        public static String LoginUrl { get; set; }
        public delegate bool CheckSessionDelegate(HttpSessionStateBase session);

        public static CheckSessionDelegate CheckSessionAlive;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            HttpSessionStateBase session = filterContext.HttpContext.Session;
            if ((CheckSessionAlive == null) || (CheckSessionAlive(session)))
                    return;


            //send them off to the login page
            var url = new UrlHelper(filterContext.RequestContext);
            var loginUrl = url.Content(LoginUrl);
            session.RemoveAll();
            session.Clear();
            session.Abandon();

            filterContext.HttpContext.Response.StatusCode = 403;
            filterContext.HttpContext.Response.Redirect(loginUrl, false);
            filterContext.Result = new EmptyResult();

        }
    }

在您的控制器中,只需在类或各个操作上添加[CheckUserSession]属性。

答案 3 :(得分:1)

这里可以找到另一个合理的解决方案:

<强> Overriding Controller Methods for session Timeout handling