How can I validate Request.Headers["Authorization"] for all controller at a single place?

Viewed 14163
[HttpGet]
public IActionResult Get()
{
    string token = Request.Headers["Authorization"];
    // Validate token.
}

[HttpPost]
public IActionResult Post(int id)
{
    string token = Request.Headers["Authorization"];
    // Validate token.
}

How can I validate Request.Headers["Authorization"] for all controller at a single place?

3 Answers

Or you could use Attributes on the class or method to validate and check the header elements. Depends on the need.

  1. Middleware
  2. Authentication Handler (similar to middleware)
  3. Custom Attributes on the Class or Method.
Related