asp.net-mvc-3 – 在MVC 3的AuthorizeAttribute中获取模型数据

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 在MVC 3的AuthorizeAttribute中获取模型数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用AuthorizeAttribute来装饰我的控制器操作.
[ServiceAuthorize(Roles="Editor,Publisher,Administrator")]
public JsonResult Create(NewsArticle newsArticle)

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

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

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

谢谢.

解决方法

Is there any way to get at the model from within the OnAuthorize
method of the AuthorizeAttribute?

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

public override void OnAuthorization(AuthorizationContext filterContext)
{
    var value = filterContext.Controller.ValueProvider.GetValue("someproperty");
    ...
}
原文链接:https://www.f2er.com/aspnet/247337.html

猜你在找的asp.Net相关文章