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.
精彩评论