If you’re just starting to develop in ASP.NET, you may not have encountered Session before. Session is a serialized collection of objects that are related to the current user’s session. The values are usually stored on the local server memory, but there are alternate architectures where the values can be stored in a SQL database or other distributed storage solutions, especially when your servers are part of a server farm. In this tutorial, we will show you how to find and use session in ASP.NET Core 1.0.
Finding Session in ASP.NET Core 1.0
ASP.NET Core 1.0 has been written from the ground up to be a modular, choose-what-you-need framework. What this means is that you must explicitly include any packages you want to use in your project. This allows developers to maintain tight control over what functionality our ASP.NET Core projects actually need, and exclude anything that is not necessary.
Step 1
Session is considered to be one of “additional” packages. In order to include that package we need to add a reference to Microsoft.AspNet.Session in the project.json file. If we wanted to use memory as our caching backend, we would also include Microsoft.Extensions.Caching.Memory.
Step 2
Once we’ve got the package included in our project, we need to make it available to the Services layer by modifying the ConfigureServices() method in the Startup file, like so:
1 2 3 4 5 6 7 8 9 10 11 | public void ConfigureServices(IServiceCollection services) { ... services.AddCaching(); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(60); options.CookieName = ".MyCoreApp"; }); ... } |
Step 3
However, that isn’t quite enough to get Session fully integrated into our project. We also need to inject the Session service into our Dependency Injection container in the Startup file’s Configure() method, like so:
1 2 3 4 5 6 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) { ... app.UseSession(); ... } |
With all of these steps completed, you can now use Session in your projects just like in any other ASP.NET application. If you wanted to use a different cache backend (rather than memory) you could grab a different NuGet package like Redis or SqlServer. Don’t forget to check NuGet if you can’t find the functionality you need; it is probably there and you just need to download it.
Using Session in ASP.NET Core 1.0
ASP.NET Core 1.0 has introduced some new extension methods that we can use for accessing and storing Session values. The odd thing is that these extensions are not in Microsoft.AspNet.Session; rather, they are in Microsoft.AspNet.Http, and so we will need to add that package.
Once we’ve got that package included, we can start using the extension methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [HttpGet] public IActionResult Index() { var userID = Context.Session.GetInt("UserID"); var userName = Context.Session.GetString("UserName"); return View(); } [HttpGet] public IActionResult Default() { Context.Session.SetInt("UserID", 5); Context.Session.SetString("UserName", "John Smith"); return View(); } |
- Get: Returns a byte array for the specified Session object.
- GetInt: Returns an integer value for the specified Session object.
- GetString: Returns a string value for the specified Session object.
- Set: Sets a byte array for the specified Session object.
- SetInt: Sets an integer value for the specified Session object.
- SetString: Sets a string value for the specified Session object.
Best and Recommended ASP.NET Core 1.0 Hosting
If you are looking at creating website of your own, hosting it with ASP.NET Core 1.0 can be a great choice. While there are various companies today who offer you ASP.NET Core 1.0 hosting services, it is extremely important for you to understand and compare the features that various companies offer. To make this search easier for you, we have listed down top 3 hosting providers which you need to compare for the best and recommended ASP.NET Core 1.0 hosting.