title: How to use Session in .net core?
body: <p>How to use Session in dot net core ?</p>
  <p>I wanted to use session tag to store the user id and username when a user is logged in. I am using .net core version 3.1
  I have created an Account Controller and in the Login(Post Method) I am trying to use session tag.
  Here is the Login Method in AccountController.cs</p>
  <pre><code> //POST Login
          [HttpPost]
          [ValidateAntiForgeryToken]
          public IActionResult Login(Account user)
          {
              if(ModelState.IsValid)
              {
                  var obj = _db.Accounts.Where(u =&gt; u.Name.Equals(user.Name) &amp;&amp; u.Password.Equals(user.Password)).FirstOrDefault();
                  if(obj != null)
                  {
                      Session[&quot;Id&quot;] = obj.Id.ToString(); // Error : The name 'Session' does not exist in the current context
                      Session[&quot;Name&quot;] = obj.Name.ToString(); // Error : The name 'Session' does not exist in the current context
                      return RedirectToAction(&quot;Index&quot;, &quot;Home&quot;);
                  }
              }
              return View(user);
          }
  </code></pre>
  <p>I have also used services.AddSession() in ConfigureServices and app.UseSession() in Configure in Startup.cs file
  Configure Services in Startup.cs</p>
  <pre><code>    public void ConfigureServices(IServiceCollection services)
          {
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseSqlServer(Configuration.GetConnectionString(&quot;DefaultConnection&quot;))
              );
  
              services.AddControllersWithViews();
              services.AddDistributedMemoryCache();
              services.AddSession(options =&gt; {
                  options.IdleTimeout = TimeSpan.FromMinutes(10);
              });
  
          }
  </code></pre>
  <p>Configure in Startup.cs</p>
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler(&quot;/Home/Error&quot;);
                  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                  app.UseHsts();
              }
  
      
              app.UseHttpsRedirection();
              app.UseStaticFiles();
  
              app.UseRouting();
  
              //app.UseAuthentication();
  
              app.UseAuthorization();
              app.UseSession();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllerRoute(
                      name: &quot;default&quot;,
                      pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
              });
          }
  </code></pre>
  <p>How do I fix this issue?</p>