简体   繁体   中英

How to create Custom Authorization attribute?

I am trying to implement a custom authorize attribute on my mvc4 app with parameters.

What I am trying to achieve is this:

[Authorize(Application == "Initialize,Start,..." , Topic == "foo1,foo2,foo3...")]
public ActionResult Edit(int id)
{
   //rest
}

The attribute should also be able to decorate the controller, not only methods

Your custom authorization attribute :

public class UberAuthorizeAttr : System.Web.DomainServices.AuthorizationAttribute
{
    public string Application {get;set;}
    public string Topic {get;set;}

    public override bool Authorize(System.Security.Principal.IPrincipal principal)
    {
        // your custom behaviour
    }
}

Your controller:

[UberAuthorizeAttr(Application = "Initialize,Start,..." , Topic = "foo1,foo2,foo3...")]
public ActionResult Edit(int id)
{
   //rest
}

Is that what you are looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM