title: How to use Session in 2 different controller
body: <p>I want to use a session in ASP.NET MVC but I think they're not connected. I have two different controllers and the <code>AccountController</code> seems fine because it's where I declared my session but I need to call it the <code>DashboardController</code> in order to prevent loading the login page again.</p>
  <p>Here is the <code>AccountController</code>:</p>
  <pre><code>    SqlConnection SqlCon = new SqlConnection();
      SqlCommand SqlCom = new SqlCommand();
      SqlDataReader dr;
  
      // GET: Account
      [HttpGet]
      public ActionResult Login()
      {
          if (Session[&quot;user&quot;] != null)
          {
              return RedirectToAction(&quot;Home&quot;, &quot;Dashboard&quot;, new { user = Session[&quot;user&quot;].ToString()});
          }
          else
          {
              return View();
          }
      }
  
      void connectionString()
      {
          SqlCon.ConnectionString = &quot;Data Source=DESKTOP-94CAMNQ;Initial Catalog=QCUPasabuyDemoDb;Integrated Security=True;&quot;;
      }
  
      [HttpPost]
      public ActionResult Verify(tbl_user acc)
      {
          connectionString();
          SqlCon.Open();   
  
          SqlCom.Connection = SqlCon;
          SqlCom.CommandText = &quot;SELECT * FROM tbl_user WHERE username='&quot; + acc.username + &quot;'AND password='&quot; + acc.password + &quot;'&quot;;
  
          dr = SqlCom.ExecuteReader();
  
          if (dr.Read())
          {
              SqlCon.Close();
              Session[&quot;user&quot;] = acc.username;
              return RedirectToAction(&quot;Home&quot;, &quot;Dashboard&quot;);
          }
          else
          {
              SqlCon.Close();
              return View(&quot;Error&quot;);
          }
      }
  </code></pre>
  <p>While this is the <code>DashboardController</code>:</p>
  <pre><code>public class DashboardController : Controller
  {
      // GET: Dashboard
      public ActionResult Dashboard()
      {
          if (Session[&quot;user&quot;] == null)
          {
              return RedirectToAction(&quot;Login&quot;, &quot;Account&quot;);
          }
          else
          {
              return RedirectToAction(&quot;Home&quot;, &quot;Dashboard&quot;, new { user = Session[&quot;user&quot;].ToString() });
          }
      }
  
      public ActionResult home()
      {
          return View(&quot;&quot;);
      }
  
      public ActionResult services()
      {
          return View();
      }
  
      public ActionResult about()
      {
          return View();
      }
  
      public ActionResult logout()
      {
          String webuser = (String)Session[&quot;user&quot;];
          Session.Abandon();
          return RedirectToAction(&quot;Login&quot;,&quot;Account&quot;);
      }
  }
  </code></pre>