1

我正在使用 MVC 4。

我编码这个继承自的客户属性System.Web.Mvc.ActionFilterAttribute

public class AuthorizedAttribute : ActionFilterAttribute
{    
    public AccessLevel Threshold { get; set; }

    public AuthorizedAttribute()
    {
        Threshold = AccessLevel.Anonymous;
    }

    public AuthorizedAttribute(AccessLevel threshold)
    {
        Threshold = threshold;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //some actions
        base.OnActionExecuting(filterContext);
    }
}

Manage在我的行动中使用它UserController

public class UserController : Controller
{
    [HttpGet]
    [Authorized(AccessLevel.Administrator)]
    public ViewResult Manage()
    {
         return View();
    }
}

OnActionExecuting我在我的属性构造函数、被覆盖的方法和我的属性构造函数中放置了一个断点,UserController当我在调试模式下通过浏览器调用操作 url 时,只有我的控制器断点被触发,即使我没有经过身份验证,我也会登陆页面。. 什么我做错了吗?

预先感谢。

4

2 回答 2

0

您的代码应该可以工作。可能您在路由等方面遇到问题。

于 2014-07-13T20:49:49.730 回答
0

看来我在 MVC 4 中并没有完全,但在 MVC 5 中几乎没有。我只需要在我的 web.config 上做一些更新来解决我的问题。我在这里找到了我的救赎

于 2014-07-15T08:47:12.810 回答