在MVC 3的AuthorizeAttribute中获取模型数据

Per*_*rry 7 action-filter asp.net-mvc-3

我正在使用AuthorizeAttribute来装饰我的控制器操作.

[ServiceAuthorize(Roles="Editor,Publisher,Administrator")]
public JsonResult Create(NewsArticle newsArticle)
Run Code Online (Sandbox Code Playgroud)

我的NewsArticle模型中有一个字段,我想在AuthorizeAttribute中的OnAuthorize方法中使用.

有没有办法从AuthorizeAttribute的OnAuthorize方法中获取模型?

我认为它可以在AuthorizationContext中的某个地方使用,但我找不到它.我知道我可以在过滤器属性的ActionExecutingContext中找到它,但这意味着我需要在我的操作上使用另一个过滤器,我希望能够在一个步骤中执行所有授权.

谢谢.

Dar*_*rov 6

有没有办法从AuthorizeAttribute的OnAuthorize方法中获取模型?

不,因为OnAuthorization在模型绑定器之前运行.您可以做的是从值提供程序中读取值:

public override void OnAuthorization(AuthorizationContext filterContext)
{
    var value = filterContext.Controller.ValueProvider.GetValue("someproperty");
    ...
}
Run Code Online (Sandbox Code Playgroud)