开发者

Asp.net mvc authorize attribute integrated with a parameter

开发者 https://www.devze.com 2023-02-03 16:57 出处:网络
I want to use an [Authorize()] attribute in the following way on an action: [Authorize(Roles = 开发者_如何转开发\"Administrator\" or UserId == id)]

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?


You can't. In .NET attributes can use only constant values. On the other hand you could write a custom authorize attribute deriving from the standard one and in the AuthorizeCore method implement this logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消