Asp.net MVC授权属性与参数综​​合 [英] Asp.net mvc authorize attribute integrated with a parameter

查看:77
本文介绍了Asp.net MVC授权属性与参数综​​合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下方式上的操作使用 [授权()] 属性:

  [授权(角色=管理员或用户ID == ID)]
公众的ActionResult编辑(INT ID){}

现在,我使用的逻辑是这样的:

 公众的ActionResult编辑(INT ID)
    {
        如果(User.IsInRole(管理员)|| User.Identity.Name.Equals(ID))
        {}
    }


解决方案

没有,但你的访问一切的功能块的控制器具有属性里面:

请参阅:

<一个href=\"http://stackoverflow.com/questions/4348071/how-to-pass-parameters-to-a-custom-actionfilter-in-asp-net-mvc-2/4348206#4348206\">How将参数传递给自定义ActionFilter在ASP.NET MVC 2?

I want to use an [Authorize()] attribute in the following way on an action:

[Authorize(Roles = "Administrator" or UserId == id)]
public ActionResult Edit(int id){ }

Right now I'm using logics like this:

    public ActionResult Edit(int id)
    {
        if (User.IsInRole("Administrator") || User.Identity.Name.Equals(id))
        { }
    }

解决方案

No, but you an access everything piece of functionality the controller has inside the Attribute:

See:

How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

这篇关于Asp.net MVC授权属性与参数综​​合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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