title: How to check whether user is logged in or not from controller in Token Based Authentication using MVC 5?
body: <p>I have <code>ActonResult Data()</code> in <code>HomeController</code>, I want to show <strong>Data.cshtml</strong> the only user already logged in. otherwise, it will return <strong>LogoutView.cshtml</strong> page</p>
  
  <p><strong>HomeController</strong></p>
  
  <pre><code>public ActionResult Data()
      {
          bool isAuthenticated = User.Identity.IsAuthenticated;
          if(isAuthenticated)
          {
              return View();
          }
          else
          {
              return View("LogoutView");
          }
      }
  </code></pre>
  
  <p>I am using Token Based Authentication in MVC5. </p>
  
  <blockquote>
    <ol>
    <li><p>I can do LogIn without any issues.</p></li>
    <li><p>Before I do login, I tried to go <strong><a href="http://localhost/Poject1/home/data" rel="nofollow noreferrer">http://localhost/Poject1/home/data</a></strong>, It shows <strong>LogoutView.cshtml</strong> as I expect.</p></li>
    <li><p>After I logged in, I tried to visit the same link <strong><a href="http://localhost/Poject1/home/data" rel="nofollow noreferrer">http://localhost/Poject1/home/data</a></strong>, It shows <strong>Data.cshtml</strong> as I expect also.</p></li>
    <li><p>But I do logged out used below code, then I tried to visit <strong><a href="http://localhost/Poject1/home/data" rel="nofollow noreferrer">http://localhost/Poject1/home/data</a></strong>, But it still shows <strong>Data.cshtml</strong> </p></li>
    </ol>
  </blockquote>
  
  <pre><code>&lt;input id="btnLogout" type="button" value="Logout" /&gt;
  &lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          $('#btnLogout').click(function () {
              console.log('Get Token = '+sessionStorage.getItem('accessToken'))
              sessionStorage.removeItem('accessToken');
              console.log('After remove Token = ' + sessionStorage.getItem('accessToken'))   // it print null
          });
      })
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Login Script</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          $('#btnSignIn').click(function () {
              var loginData = {
                  grant_type: 'password',
                  username: $('#txtSignInEmail').val(),
                  password: $('#txtSignInPassword').val()
              };
  
              $.ajax({
                  type: 'POST',
                  url: 'Token',
                  data: loginData
              }).done(function (data) {
  
                  console.log('Username =='+data.userName);
  
                  $('#user').text(data.userName)
                  $('#UserName').text(JSON.stringify(data.userName));
                  // Cache the access token in session storage.
                  console.log('Login Success');
                  sessionStorage.setItem('accessToken', data.access_token);
              }).fail(function (showError) {
                  $('#signInError').text(JSON.stringify(showError));
              });
          });
      })
  &lt;/script&gt;
  </code></pre>