title: Implementing PHP session in MVC C#
body: <p>Currently, I am working on a web application using MVC C#. I would like to ask if there is a way to implement a PHP Session in MVC. The use of the session I am talking about is like, when you write only the link or path like this from <code>https://localhost:44360/Login/Login</code> to <code>https://localhost:44360/Home/Home</code> to go to the <code>Home</code> Page of the Web even if you did not sign-in using an account you will not be redirected to that page.</p>
  <p>After I logged in to an account, I am redirected to the homepage which is correct, after that, I logged out the account and try to Type <code>https://localhost:44360/Home/Home</code> and unfortunately it was redirected or even clicking the back button of the browser.</p>
  <p>I am currently working on the code below.</p>
  <p><strong>LoginController.cs</strong></p>
  <pre><code> [HttpPost]
          public ActionResult Login(LoginModel userInfo, FormCollection collection, string returnUrl)
          {
              ILogicInterface&lt;LogicalUserInput, LogicalSystemResult&gt; iLogic = new UserLoginCheck();
              LogicalUserInput userInput = new LogicalUserInput();
              _ = new LogicalSystemResult();
              try
              {
                  userInput[typeof(LoginModel).FullName] = userInfo;
                  LogicalSystemResult systemResult = iLogic.DoProcess(userInput);
                  bool userCheckExist = systemResult.ResultCode != LogicalSystemResult.RESULT_CODE_ERR_DATA_NOT_EXIST;
  
                  if (userCheckExist)
                  {
                      UserLoginModel loginModel = systemResult[typeof(UserLoginModel).FullName] as UserLoginModel;
                      Session[&quot;userInfo&quot;] = loginModel;
                      FormsAuthentication.SetAuthCookie(loginModel.email, true);
  
                      if (!string.IsNullOrEmpty(returnUrl))
                      {
                          return Redirect(returnUrl);
                      }
                      else
                      {
                          if (loginModel.AccountType == 0) {
                              return RedirectToAction(&quot;Home&quot;, &quot;Home&quot;);
                          } else {
                              return RedirectToAction(&quot;Error&quot;, &quot;Error&quot;);
                          }                   
                      }
                  }
                  else
                  {
                      TempData.Clear();
                      TempData[&quot;Title&quot;] = &quot;Error!&quot;;
                      TempData[&quot;Message&quot;] = &quot; Invalid Username Or Password.&quot;;
                      return View();
                  }
  
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  return RedirectToAction(&quot;Error&quot;, &quot;Error&quot;);
              }
          }
  </code></pre>
  <p><strong>Logout Button in HOME.cshtml</strong></p>
  <pre><code>&lt;a id=&quot;cmdLogOff&quot;&gt;
     &lt;i class=&quot;feather icon-log-out&quot;&gt;&lt;/i&gt;Logout
  &lt;/a&gt;
  </code></pre>
  <p><strong>Click Event of LogoutButton</strong></p>
  <pre><code>&lt;script&gt;
      $(&quot;#cmdLogOff&quot;).on(&quot;click&quot;, function () {
          $(&quot;#HomeView&quot;).submit();
      });
  &lt;/script&gt;
  
  @using (Ajax.BeginForm(&quot;LogOut&quot;,
           null,
           new AjaxOptions
           {
           },
      new { id = &quot;HomeView&quot; }))
  {
     
  }
  </code></pre>
  <p><strong>HOMEController.cs</strong></p>
  <pre><code> [Authorize]
      public ActionResult Home()
      {
          if (Session[&quot;userInfo&quot;] == null) {
              return RedirectToAction(&quot;Error&quot;, &quot;Error&quot;);
          } else {
              UserLoginModel userLoginModel = Session[&quot;userInfo&quot;] as UserLoginModel;
              TempData[&quot;user&quot;] = userLoginModel.lastName + &quot;, &quot; + userLoginModel.firstName + &quot; &quot; + userLoginModel.middleName; 
              return View();
          }         
      }
  
      [Authorize]
      [HttpPost]
      public ActionResult LogOut() {
          try {
                  Session.Abandon();
                  FormsAuthentication.SignOut();
                  Session[&quot;userInfo&quot;] = null;
              return RedirectToAction(&quot;Login&quot;, &quot;Login&quot;);
          } catch (Exception ex) {
              Console.WriteLine(ex.Message);
              return View();
          }
      }
  </code></pre>
  <p>The main question here is that, how can I not redirect the user if he/she just only type the link he/she wants to access without an account. And after logging out of an account, the Home Page will not be shown even if the user clicked the back of the browser?</p>