title: Azure AD : how to redirect to sign in page by code manually
body: <p>I followed this <a href="https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1-Call-MSGraph" rel="nofollow noreferrer">official sample</a> to integrate Azure AD authentication into my ASP.NET Core application. Everything is working well, and I can call the MS Graph API successfully.</p>
  <p>The problem is that after I signed in, I kept the browser opening and then I shutdown my application (simulate the server crashed), then I run the application again, then refreshing the page, I got an exception as shown in this screenshot:</p>
  <p><a href="https://i.stack.imgur.com/G9pUg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G9pUg.png" alt="enter image description here" /></a></p>
  <p>I debugged the code, and I found that the request went into my controller (I had <code>[Authorize]</code> on my controller so it should redirect to sign in page when app found the request doesn't be authenticated) and the exception appeared when run</p>
  <pre><code>var me = await _graphClient.Me.Request().GetAsync();
  </code></pre>
  <p>This means the app thinks the request is authenticated.</p>
  <pre><code>[Authorize]
  public class HomeController : Controller
  {
      private readonly ILogger&lt;HomeController&gt; _logger;
      private readonly GraphServiceClient _graphClient;
  
      public HomeController(GraphServiceClient graphClient, ILogger&lt;HomeController&gt; logger)
      {
          _logger = logger;
          _graphClient = graphClient;
      }
      
      public async Task&lt;IActionResult&gt; IndexAsync()
      {
          var me = await _graphClient.Me.Request().GetAsync();
          ViewBag.Myname = me.DisplayName;
          return View();
      }
  }
  </code></pre>
  <p>The reason for why the app not redirected to sign in page is that the browser stored cookie, after removing the cookie manually, the exception wouldn't appear and app would redirect to sign in page.</p>
  <p>I think I need a global filter to handle the exception, by removing the cookie (I'm not sure if it can remove the cookie) and then redirect to sign in page manually, but I failed to find any document to describe how to redirect to sign in page manually by code.</p>
  <p>Any document or code sample is appreciated.</p>
  <p>By the way, should we put effort on this kind of scenario?</p>