title: Custom AuthorizeAttribute OnAuthorizationAsync Function
body: <p>I'm trying to make my own custom <code>System.Web.Http.AuthorizeAttribute</code>. Everything was working until I needed to run some <code>async</code> function inside of the overridden <code>OnAuthorization</code>function.</p>
  
  <p>I found the <code>OnAuthorizationAsync</code> method and I use that, however still having issues running an <code>async</code> method. I need to ensure that a header attached to the <code>HttpActionContext</code> is in the db.</p>
  
  <pre><code>    public override Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
      {
          IEnumerable&lt;string&gt; values;
          if (actionContext.Request.Headers.TryGetValues("Authentication", out values))
          {
              var token = new Token() { TokenString = values.First() };
              if (await _tg.ValidateToken(token))
              {
                  return base.OnAuthorizationAsync(actionContext, cancellationToken);
              }
          }
  
          base.HandleUnauthorizedRequest(actionContext);
      }
  </code></pre>
  
  <hr>
  
  <p><code>_tg.ValidateToken(token)</code> is an <code>async</code> method that returns a bool.  If I try to <code>await</code> it and make <code>OnAuthorizationAsync</code> an <code>async</code> method I then apparently cant return a Task:</p>
  
  <blockquote>
    <p>Since OnAuthorizeAsync is an <code>async</code> method that returns 'Task', a return keyword must not be followed by an object expression.</p>
  </blockquote>
  
  <p>Is there a way you can see out of this mess?</p>