title: Redirect in .NET Core OpenID Connect middleware?
body: <p>I'm trying to handle a scenario when the OpenID Connect server I'm authenticating to returns a particular set of querystrings. When the condition matches, I want to essentially redirect the user to an "Access Denied" page. For whatever reason the commented line below containing the redirect never actually fires. Is there a better/different way to do what I'm after?</p>
  
  <p>Here's how the OpenID Connect middleware is configured in <strong>Startup.cs</strong>:</p>
  
  <pre><code>services.Configure&lt;OpenIdConnectOptions&gt;(options =&gt; 
  {
  
      // ...
  
      options.Events = new OpenIdConnectEvents
      {
          OnMessageReceived = context =&gt;
          {
              if (context.HttpContext.Request.Query.ContainsKey("error"))
              {
                  context.HandleResponse(); // &lt;-- Fires
                  context.Response.Redirect("/AccessDenied"); // &lt;-- Redirect fires but user is not redirected
              }
  
              return Task.FromResult(0);
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> Got it working with the following tweaks:</p>
  
  <pre><code>options.Events = new OpenIdConnectEvents
  {
      OnRemoteFailure = context =&gt;
      {
          context.HandleResponse();
          context.Response.Redirect("AccessDenied?error=" + context.Failure.Message);
  
          return Task.FromResult(0);
      },
      // ...
  };
  </code></pre>