title: JwtBearerEvents.OnMessageReceived not Called for First Operation Invocation
body: <p>I am using WSO2 as my Identity Provider (IDP).  It is putting the JWT in an header called "X-JWT-Assertion".  </p>
  
  <p>To feed this into the ASP.NET Core system, I added an <code>OnMessageReceived</code> event.  This allows me to set the <code>token</code> to the value supplied in the header.</p>
  
  <p>Here is the code that I have to do that (the key part is the last 3 lines of non-bracket code):</p>
  
  <pre><code>services.AddAuthentication(options =&gt;
  {
      options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
      options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  })
  .AddCookie()
  .AddJwtBearer(async options =&gt;
  {
      options.TokenValidationParameters = 
           await wso2Actions.JwtOperations.GetTokenValidationParameters();
  
      options.Events = new JwtBearerEvents()
      {
          // WSO2 sends the JWT in a different field than what is expected.
          // This allows us to feed it in.
          OnMessageReceived = context =&gt;
          {
              context.Token = context.HttpContext.Request.Headers["X-JWT-Assertion"];
              return Task.CompletedTask;
          }
      }
  };
  </code></pre>
  
  <p><strong>This all works perfectly</strong> except for the very first call after the service starts up.  To be clear, every call, except for the first one works exactly as I want it to.  (It puts the token in and updates the <code>User</code> object like I need.)</p>
  
  <p>But for the first call, the <code>OnMessageReceived</code> is not hit.  And the <code>User</code> object in my controller is not setup.</p>
  
  <p>I checked <code>HttpContext</code> for that first call, and the "X-JWT-Assertion" header is in the <code>Request.Headers</code> list (with the JWT in it).  But, for some reason, the <code>OnMessageReceived</code> event is not called for it.</p>
  
  <p>How can I get <code>OnMessageReceived</code> to be called for the first invocation of a service operation for my service?</p>
  
  <p><strong>IMPORTANT NOTE:</strong> I figured out that the issue was <code>async</code> <code>await</code> in <code>AddJwtBearer</code>.  (See my answer below.) That is what I really wanted out of this question.  </p>
  
  <p>However, since a bounty cannot be cancled, I will still award the bounty to anyone who can show a way to use <code>AddJwtBearer</code> with <code>async</code> <code>await</code> where it is awaiting an actual <code>HttpClient</code> call.  Or show documentation of why <code>async</code> <code>await</code> is not supposed to be used with <code>AddJwtBearer</code>.</p>