Home > database >  Priority of loading attribute in MVC Controller
Priority of loading attribute in MVC Controller

Time:07-14

Im trying to implement Attribute base premission on my controller/action.

this is what action look like :

    [HttpPost]
    [ValidateHeaderAntiForgeryToken]
    [ValidateInput(false)]
    [Authorize]
    [Permission(ModuleId = (int)Enums.ActionOne]
    [GraphAntiXss(DataInput = "data")]
    public ActionResult ActionOne(MyModel data)
    {
        .........
    }

Now problem is if i call the action , the Permission attribute also checked out , even if

user not Authenticated yet(Im using Identity). is there any way to avoid other attribute

getting called before user Authentication?

Also if i put [Authorize] on top of controller , program redirect me to some page with

error "login.aspx". (im on mvc and dont have this page)

Edit: Im using old MVC5(before dotnet core).

CodePudding user response:

For every attribute you put in action or controller, it works with AND operator, so the sequence in the matter which is called first doesn't matter

1 AND 1 AND 0 AND 1

same thing as

0 AND 1 AND 1 AND 1

and so on

For filter attribute, you can use Order

[somefilter(Order = 2)]
[otherfilter(Order = 1)]

To execute some code before any controller or action, you can fiddle with middleware where code can be executed along the pipeline between request and response. Here sequence do matter

  • Related