ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. Sessions values can be stored for the duration of the visitor’s session on your site. Most cases, they are stored in server memory. You can configure session to store either in State server or in SQL Server. In ASP.NET MVC, you can create and access session variables using HttpContext.Current.Session. In ASP.NET 5, ASP.NET team implemented a middleware to support session management.
How to use ASP.NET 5 session middleware.
First include “Microsoft.AspNet.Session”: “1.0.0-*”, reference in the project.json file.
In Startup.cs include the add the session middleware to the ConfigureServices() method.
Also in the configure() method, you need to add UseSession() extension method.
Add you can manage the session via Context object.
There are three methods that enable you to set session values: SetInt, SetString and Set, which takes a byte array as an argument. And three methods for get session values as well, GetInt, GetString and Get, which returns byte array for the key. If you want to store custom types in session, you can use Get / Set methods, and you need to implement the serialization and de-serialization logic.
Happy Programming :)
Did you like this article? Share it with your friends