title: AddJwtBearer OnAuthenticationFailed return custom error
body: <p>I am using <a href="https://github.com/openiddict/openiddict-core" rel="noreferrer">Openidict</a>.<br>
  I am trying to return custom message with custom status code, but I am unable to do it. My configuration in <code>startup.cs</code>:</p>
  
  <pre><code>services.AddAuthentication(options =&gt;
              {
                  options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                  options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
              })
              .AddJwtBearer(o =&gt;
              {
                  o.Authority = this.Configuration["Authentication:OpenIddict:Authority"];
                  o.Audience = "MyApp";           //Also in Auhorization.cs controller.
                  o.RequireHttpsMetadata = !this.Environment.IsDevelopment();
                  o.Events = new JwtBearerEvents()
                  {
                      OnAuthenticationFailed = context =&gt;
                      {
                          context.Response.StatusCode = HttpStatusCodes.AuthenticationFailed;
                          context.Response.ContentType = "application/json";
                          var err = this.Environment.IsDevelopment() ? context.Exception.ToString() : "An error occurred processing your authentication.";
                          var result = JsonConvert.SerializeObject(new {err});
                          return context.Response.WriteAsync(result);
                      }
                  };
              });
  </code></pre>
  
  <p>But the problem is no content is returned. Chrome developer tools report </p>
  
  <blockquote>
    <p>(failed)</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/zeGYr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zeGYr.png" alt="error"></a></p>
  
  <p>for Status and </p>
  
  <blockquote>
    <p>Failed to load response data</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/Nzizx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Nzizx.png" alt="error"></a></p>
  
  <p>for response.</p>
  
  <p>I also tried:</p>
  
  <pre><code>context.Response.WriteAsync(result).Wait();
  return Task.CompletedTask;
  </code></pre>
  
  <p>but the result is the same.</p>
  
  <p><strong>Desired behaviour:</strong><br>
  I would like to return custom status code with message what went wrong.</p>