

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to enable Session in C# ASP.NET Core?
Session is a feature in ASP.NET Core that enables us to save/store the user data.
Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.
The SessionId cookie is per browser and it cannot be shared between the browsers.
There is no timeout specified for SessionId cookie and they are deleted when the Browser session ends.
At the Server end, session is retained for a limited time. The default session timeout at the Server is 20 minutes but it is configurable.
Microsoft.AspNetCore.Session package provides middleware to manage the sessions in ASP.NET Core. To use session in our Application, we need to add this package as a dependency in project.json file.
The next step is to configure session in Startup class.
We need to call "AddSession" method in ConfigureServices method of startup class.
The "AddSession" method has one overload method, which accepts various session options such as Idle Timeout, Cookie Name and Cookie Domain etc.
If we do not pass the session options, the system will take the default options
Example
public class Startup { public void Configure(IApplicationBuilder app){ app.UseSession(); app.UseMvc(); app.Run(context => { return context.Response.WriteAsync("Hello World!"); }); } public void ConfigureServices(IServiceCollection services){ services.AddMvc(); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(60); }); } }
How to access the session
public class HomeController : Controller{ [Route("home/index")] public IActionResult Index(){ HttpContext.Session.SetString("product","laptop"); return View(); } [Route("home/GetSessionData")] public IActionResult GetSessionData(){ ViewBag.data = HttpContext.Session.GetString("product");; return View(); } }
- Related Questions & Answers
- How to handle errors in middleware C# Asp.net Core?
- What is Kestral C# Asp.net Core?
- What is routing in C# ASP.NET Core?
- What is Metapackage in C# Asp.net Core?
- Explain how logging works in ASP.NET Core
- How C# ASP.NET Core Middleware is different from HttpModule?
- How to enable Web view session storage in android?
- How do you configure ASP.NET Core applications?
- How to schedule background tasks (jobs) in ASP.NET Core?
- Explain how Razor Pages work in ASP.NET Core
- Explain how error handling works in ASP.NET Core
- Explain how to create a new ASP.NET Core project
- What are the benefits of choosing ASP.NET Core over ASP.NET?
- Explain how static files are served in ASP.NET Core
- What is the use of UseIISIntegration in C# Asp.net Core?