Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnAuthorization Method of AuthorizationFilterAttribute will execute every time when request arrives?

I am writing Web API ( v2.2 ) for accessing another REST API. Reason is that I want restrict some functional and provide more friendly data. In short I am writing wrapper for some REST API. I am providing authentication/authorization via my Implementation of AuthorizationFilterAttribute. Here is snippet of code:

public override void OnAuthorization(HttpActionContext actionContext)
{
    var authorizeHeader = actionContext.Request.Headers.Authorization;
    if (authorizeHeader != null
        && authorizeHeader.Scheme.Equals("basic", StringComparison.OrdinalIgnoreCase)
        && String.IsNullOrEmpty(authorizeHeader.Parameter) == false)
    {
      // Code to test is username/password correct for second API 
      // Trying to get some recourse with provided credentials
      // ...
    }
}

Now I want to understand, if I set my attribute to controller's action, like this:

[HttpGet]
[Route("{taskId}/{dispatchId}")]
[SecureResourceAttribute] // This is my attribute
public IHttpActionResult GetDispatch(string taskId, string dispatchId)

Every time when I request this action, OnAuthorization method will execute ? Does it anywhere store that client is already authorized ? If No, how can I get it to store that client is already authorized ?

Thank you, for your time.

like image 805
bot_insane Avatar asked Nov 28 '25 11:11

bot_insane


1 Answers

Q: Is OnAuthorization going to be called on every call?

A: If controller is decorated with [Authorize] attribute, Yes.

Q: Does it anywhere store that client is already authorized?

A: No. The service is Stateless by default.

Q: If No, how can I get it to store that client is already authorized?

A: This again goes back to the fact that WEB API Service is stateless and there is no relationship between different requests that comes in.

like image 127
Aram Avatar answered Dec 01 '25 01:12

Aram