ASP.NET MVC 3 OnActionExecuting导致无限循环 [英] ASP.NET MVC 3 OnActionExecuting causes infinite loop

查看:44
本文介绍了ASP.NET MVC 3 OnActionExecuting导致无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有该重写的OnActionExecuting方法(以检查是否在用户登录之前执行动作)

I have that overriden OnActionExecuting method (to check before action execute if user is logged in)

public class AuthenticationAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        { 
            string redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery);

            filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true);
        }
        else 
            base.OnActionExecuting(filterContext);
    }
}

为什么-如果用户未登录-响应将再次重定向到该方法.为什么?

Why - if user is not logged in - the response is redirected to that method again. Why ?

推荐答案

这可能是因为您要重定向到的控制器操作(我认为登录网址)也装饰有此属性.因此,如果用户未通过身份验证,则将其重定向到登录操作,并且由于用户未通过身份验证,因此将其重定向到登录操作,依此类推.我个人建议您使用[Authorize]属性,而不要编写这样的动作过滤器.

That's probably because the controller action that you are redirecting to (the login url I think) is also decorated with this attribute. So if the user is not authenticated he gets redirected to the login action and because he is not authenticated he gets redirected to the login action and so on. Personally I would recommend you using the [Authorize] attribute instead of writing such action filter.

这篇关于ASP.NET MVC 3 OnActionExecuting导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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