title: ASP.NET 5/MVC 6 Redirect During Authentication Middleware Events
body: <p>What I am wanting to do is create a database record and redirect a user to a special page the first time that they sign into a site - regardless of the original request URL.  Subsequent signins should update a last logged in field in the database.</p>
  
  <p>I'm using an external authentication source with Cookie Authentication.  It seems intuitive to do these things during the <code>OnSignedIn</code> event for the Cookie Authentication.  The database parts of this process work fine.  The problem redirecting to the special landing page during their first ever signin. </p>
  
  <p>Here is how the Cookie Authentication is configured:</p>
  
  <pre><code>app.UseCookieAuthentication(options =&gt;
  {   
      options.AuthenticationScheme = "Cookies";
      options.AutomaticAuthenticate = true;
      options.AutomaticChallenge = true;
  
      options.LoginPath = "/login";
      options.AccessDeniedPath = "/access-denied";
  
      options.Events = new CookieAuthenticationEvents
      {
          OnSignedIn = context =&gt;
          {
              var userRecord = userService.GetUser(context.Principal.Identity.Name);
  
              if (userRecord == null)
              {
                  userService.CreateUser(context.Principal.Identity.Name);
  
                  // Does set a cookie successfully.
                  context.Response.Cookies.Append("FirstTimeUser", "true");   
                  // Does not redirect but continues on to the originally requested URL.
                  context.Response.Redirect("/special-first-timer-page");     
              }
              else
              {
                  userService.RecordLogin(userRecord);
              }
  
              return Task.FromResult(0);
          }
      };
  });         
  </code></pre>
  
  <p>I've worked around this by adding an additional piece of middleware and leveraging the fact that I can set a cookie during the <code>OnSignedIn</code> event.  </p>
  
  <pre><code>app.Use(async (context, next) =&gt;
  {
      if (context.Request.Cookies.ContainsKey("FirstTimeUser"))
      {
          context.Response.Cookies.Delete("FirstTimeUser");
          context.Response.Redirect("/special-first-timer-page");
      }
  
      await next();
  });
  
  app.UseCookieAuthentication(options =&gt;
  {
      ...
  
      options.Events = new CookieAuthenticationEvents
      {
          OnSignedIn = context =&gt;
          {
              var userRecord = userService.GetUser(context.Principal.Identity.Name);
  
              if (userRecord == null)
              {
                  userService.CreateUser(context.Principal.Identity.Name);
                  context.Response.Cookies.Append("FirstTimeUser", "true");
              }
              else
              {
                  userService.RecordLogin(userRecord);
              }
  
              return Task.FromResult(0);
          }
      };
  });
  </code></pre>
  
  <p>This works, but doesn't seem ideal.  Is it possible to successfully redirect during an Authentication middleware event?  Am I missing a point even trying to do so?</p>