4

I have a global authorization filter with overridden OnAuthorization method.

On debugging, I see that the controller class is instantiated first and then the OnAuthorization method is invoked.

Is this a known behavior as I have some code in the controller constructor which need not run if not authorized.

Is there any way to work around this?

1
  • what exactly is your constructor doing - are you sure it isn't doing too much? Generally speaking, your constructor shouldn't be dealing with authorisation concerns at all. Commented May 29, 2014 at 7:41

1 Answer 1

5

Action filter are executed just before the Action on which they are applied to. If you have global Authorization filter, that means they are registered globally and you don't have to decorate each action with those filters, but that does not change behavior of Action filters i.e. to execute just before the action.

Controllers are initialized first and then corresponding Actions are called, so your authorization filter will be executed after Constructor of Controller and just before the action. This is behaviour of Action filters as they are designed so.

If you want to execute authorization before controller construct, try creating Message handler for authorization instead of Action filter by inheriting DelegatingHandler class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.