加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 04:08:38 所属栏目:asp.Net 来源:网络整理
导读:我正在使用AuthorizeAttribute来装饰我的控制器操作. [ServiceAuthorize(Roles="Editor,Publisher,Administrator")]public JsonResult Create(NewsArticle newsArticle) 我的NewsArticle模型中有一个字段,我想在AuthorizeAttribute中的OnAuthorize方法中使用
我正在使用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");
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读