title: C# Extending the Authenticate Attribute (ASP.NET Core 6)
body: <p>I need to add a flag into the request <code>HttpContext.Items.Add(&quot;Flag&quot;,&quot;FlagValue&quot;)</code> after the user authenticates. I have done similar tasks like this using a separate <code>ActionFilterAttribute</code> but wanted to do in by extending or overriding the <code>AuthorizeAttribute</code>.</p>
  <p>I know this can be done using by creating a custom authorize attribute and I've looked at a lot of examples online but the sample I've found doesn't seem to work in ASP.NET Core 6 MVC.</p>
  <p>I started something simple to see if it authenticates</p>
  <pre><code>[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
  public class AuthorizeUserAttribute : AuthorizeAttribute  
  {
      public AuthorizeUserAttribute() 
      {
          Log.Information(&quot;Authenticated?&quot;); // stops here
      }
  
      public AuthorizeUserAttribute(string policy) : base(policy) 
      {
          Log.Information(&quot;Authenticated?&quot;); // doesn't stop here
      }
  }
  </code></pre>
  <p>and is getting authenticated and checking on the Policies I've created.</p>
  <pre><code>[CheckIsAppLocked]
  [HttpGet(&quot;connections&quot;)]
  [AuthorizeUser(Policy = &quot;App_User&quot;)]
  public ActionResult&lt;Packet&lt;List&lt;SFTP_Conn_Info&gt;&gt;&gt; GetConnInfo() 
  {
      return OK(HomeLogic.GetConnInfo());
  }
  </code></pre>
  <p>My problem is that I cannot tap into the <code>HttpContext</code> class. I don't know the methods I can expose with <code>AuthorizeAttribute</code>, I even tried using the <code>HttpContextAccessor</code> class but it returns null (but when used with the an action filter is not null)</p>
  <p>I can call in the action filter <code>OnActionExecuting(ActionExecutingContext actionContext)</code> and reference the actionContext.HttpContext class.</p>
  <p>my guess there is a method I need to define in my AuthorizeAttribute but not sure which one is supported in .NET 6.</p>
  <p>Help appreciated!</p>