title: Logout of MVC Application From Angular 5
body: <p>We have an angular 5 application built on top of MVC authentication.  The application is served up from the Home/Index action, and once the application is loaded, the angular routing takes care of pretty much everything.  We are doing this primarily because we wanted to use MVC's Authentication processes (we are using Identity Server 4 as our Oath system).</p>
  
  <p>This works well with one exception: logout.  When we attempt to logout, the application seems to be immediately reauthorized and reloads instead of returning us to our Identity Server login page.  </p>
  
  <p>Originally, we had success in our development environment through this code:  </p>
  
  <pre><code>[HttpPost]
  public async Task&lt;IActionResult&gt; Logout()
  {
    foreach (string key in Request.Cookies.Keys)
    {
      Response.Cookies.Delete(key);
    }
  
    await HttpContext.SignOutAsync();
  
    return Ok();
  }
  </code></pre>
  
  <p>But it was a false positive, because all of our applications were running on localhost, so they had access to each other's cookies.  Due to this, the Identity Server cookies were cleared along with the cookies for the Angular application.</p>
  
  <p>We attempted to replace that logic with something like this:  </p>
  
  <pre><code>    public async Task Logout()
      {
          if (User?.Identity.IsAuthenticated == true)
          {
              // delete local authentication cookie
              await HttpContext.SignOutAsync("Cookies");
              await HttpContext.SignOutAsync("oidc");
          }
      }
  </code></pre>
  
  <p>However, it did not log out in either environment (however that code works for one of our MVC applications using the same identity server).  </p>
  
  <p>To give some background, our logout process for the Angular application comes from a material menu, where we then prompt the user if they really want to logout with a modal, before calling the logout function.  Code snippets of this process:</p>
  
  <p>Method called by logout button:</p>
  
  <pre><code>public openDialog(): void {
      let dialogRef = this.dialog.open(ModalComponent, {
        width: '250px',
        data: { text: this.logoutText, ok: this.okText, cancel: this.cancelText }
      });
  
      dialogRef.afterClosed().subscribe(result =&gt; {
        if (result !== undefined) {
          switch (result.data) {
            case 'ok':
              this.logout();
              break;
            case 'cancel':
              break;
            default:
              break;
          }
        }
     });
  }
  
  private logout(): void {
     this.sessionService.logOut();
  }
  </code></pre>
  
  <p>Session service:</p>
  
  <pre><code>public logOut(): void {
      this.auth.revokeToken();
      this.http.post(this.logoutUrl, undefined).subscribe(x =&gt; window.location.reload());
  }
  </code></pre>
  
  <p>As mentioned, calling logout this way ends up with a page refresh and the user not really being logged out.  There's probably something simple we are missing, but all of the tinkering I've done so far has not led to any success.</p>
  
  <p>EDIT:</p>
  
  <p>Our angular routing is fairly simple (though the pathing prevents us from calling something like home/logout):</p>
  
  <pre><code>{
    path: 'parts',
    component: PartsComponent,
    canActivate: [AuthGuard],
    runGuardsAndResolvers: 'always',
    data: { title: 'Parts' }
  },
  {
    path: '',
    redirectTo: 'parts',
    pathMatch: 'full'
  },
  {
    path: '**',
    redirectTo: 'parts'
  }
  </code></pre>
  
  <p>Our MVC routes are also fairly straightforward</p>
  
  <pre><code>    app.UseMvc(routes =&gt;
      {
        routes.MapRoute(
         name: "default",
         template: "{controller=Home}/{action=Index}/{id?}");
  
        routes.MapRoute(
         "Sitemap",
         "sitemap.xml",
         new { controller = "Home", action = "SitemapXml" });
  
        routes.MapSpaFallbackRoute(
          name: "spa-fallback",
          defaults: new { controller = "Home", action = "Index" });
      });
  </code></pre>
  
  <p>I'm not certain how we could easily route to home/logout with the angular routes the way they are.  I'm guessing we'd have to add a route for it.  We tried that at one point but it never routed correctly.  I'm trying to find my notes on what we tried.</p>