title: Log to database when authorization fail on Web API [Net.Core]
body: <p>I have a .Net Core Web API with Custom Policy-Based Authorization (policies that check for claims and claims values on a JWT).</p>
  
  <p>I need to log (to a database) every time a user call a Web API function, even when the authorization fail (especially on failures).</p>
  
  <p>To do that I override these methods <code>OnActionExecuting</code> and <code>OnResultExecuted</code> (because i want to log every request on one filter, and, in other filter, the result of the request). But, if the Authorization fail, both filters never were trigged.</p>
  
  <p>That's because the Authorization filter goes before the other filters (and if the request is unauthorized, short-circuit the pipeline).</p>
  
  <p>So, when a user call a method without authorization I can't log it (i'm using Serilog to write a log file on the server, and works fine when authorization fail, but I want to log it to the database like when authorization is fine).</p>
  
  <p>I try to write some filter before or after Authorization but I couldn't do it. May be i'm not thinking in the right way, and is more simple.</p>
  
  <p>I read all this, but i couldn't figured out yet:</p>
  
  <ul>
  <li><p><a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters" rel="nofollow noreferrer">ASP .Net Core Filters</a></p></li>
  <li><p><a href="https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies" rel="nofollow noreferrer">ASP .NET Core Custom Policy-Based Authorization</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/37276005/asp-net-core-policy-based-authorization-ends-with-401-unauthorized">Asp.Net Core policy based authorization ends with 401 Unauthorized</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/31464359/how-do-you-create-a-custom-authorizeattribute-in-asp-net-core">How do you create a custom AuthorizeAttribute in ASP.NET Core?</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/41526086/custom-authorization-attribute-in-net-core">Custom authorization attribute in .NET Core</a></p></li>
  </ul>
  
  <p>Thanks!</p>