Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Session in ASP.NET Core via static variable?

In earlier version of Asp.Net session can be accessed in any page like a static variable using

System.Web.HttpContext.Current.Session["key"]

In Asp.Net Core, How to access session in different class called via controller, without passing the session property as an additional parameter in the constructor of all classes

like image 695
Navaneeth Avatar asked Jun 13 '16 09:06

Navaneeth


People also ask

How can we use session in static method?

Just use the below syntax: HttpContext. Current. Session["SessionName"].

Can we use session in .NET core?

ASP.NET Core maintains session state by providing a cookie to the client that contains a session ID. The cookie session ID: Is sent to the app with each request. Is used by the app to fetch the session data.

What methods are used to enable session in ASP.NET Core?

This article will cover the steps to enable Session Sate in ASP.Net Core applications. Session can be enabled using the Configure method. Inside this method, you will have to call the UseSession method of the app object. Note: It is mandatory to call the UseSession method before the UseMvc method.

What is IHttpContextAccessor?

It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any. ASP.NET Core applications access the HTTPContext through the IHttpContextAccessor interface. The HttpContextAccessor class implements it.


2 Answers

Revised approach 1/17/17 to fix bug

First, I'll assume that you have your ASP.NET Core application configured to use session state. If not see @slfan's answer How to access the Session in ASP.NET Core via static variable?

How to access session in different class called via controller, without passing the session property as an additional parameter in the constructor of all classes

Asp.Net Core is designed around dependency injection and in general the designers didn't provide much static access to context information. More specifically there is no equivalent to System.Web.HttpContext.Current.

In Controllers you can get access to Session vars via this.HttpContext.Session but you specifically asked how to get access to the session from methods called by the controller without passing the session property as a parameter.

So, to do this we need to setup our own static class to provide access to session and we need some code to initialize that class at startup. Since it's likely that a person may want static access to the whole HttpContext object and not just the Session I took that approach.

So first we need the static class:

using Microsoft.AspNetCore.Http; 
using System;
using System.Threading;

namespace App.Web {

    public static class AppHttpContext {
        static IServiceProvider services = null;

        /// <summary>
        /// Provides static access to the framework's services provider
        /// </summary>
        public static IServiceProvider Services {
            get { return services; }
            set {
                if(services != null) {
                    throw new Exception("Can't set once a value has already been set.");
                }
                services = value;
            }
        }

        /// <summary>
        /// Provides static access to the current HttpContext
        /// </summary>
        public static HttpContext Current {
            get {
                IHttpContextAccessor httpContextAccessor = services.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;
                return httpContextAccessor?.HttpContext;
            }
        } 

    }
}

Next we need to add a service to the DI container that can provide access to the current HttpContext. This service ships with the Core MVC framework but is not installed by default. So we need to "install" it with a single line of code. This line goes in the ConfigureServices method of the Startup.cs file and can be located anywhere in that method:

//Add service for accessing current HttpContext
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();    

Next we need to setup our static class so that it has access to the DI container to obtain the service we just installed. The code below goes in the Configure method of the Startup.cs file. This line can be be located anywhere in that method:

AppHttpContext.Services = app.ApplicationServices; 

Now any method called by the Controller, even via the async await pattern, can access the current HttpContext via AppHttpContext.Current

So, if we use the Session extension methods in the Microsoft.AspNetCore.Http namespace, we can save an int called "Count" to session could be done like this:

AppHttpContext.Current.Session.SetInt32("Count", count);

And retrieving an int called "Count" from session could be done like this:

int count count = AppHttpContext.Current.Session.GetInt32("Count");

Enjoy.

like image 53
RonC Avatar answered Oct 24 '22 12:10

RonC


In Startup.ConfigureServices you have to add the service

services.AddSession();

and in the method Configure you have to use it (important: call before app.UseMvc())

app.UseSession();

Now you can use it in your controllers (if derived from Controller). You can store

var data = new byte[] { 1, 2, 3, 4 };
HttpContext.Session.Set("key", data); // store byte array

byte[] readData;
HttpContext.Session.TryGetValue("key", out readData); // read from session

When you import the namespace Microsoft.AspNetCore.Http then you can use SetString and SetInt32 as well.

using Microsoft.AspNetCore.Http;

HttpContext.Session.SetString("test", "data as string"); // store string
HttpContext.Session.SetInt32("number", 4711); // store int

int ? number = HttpContext.Session.GetInt32("number");

Outside the controller you do not have access to the HttpContext, but you can inject an IHttpContextAccessor instance like described in this answer

like image 28
slfan Avatar answered Oct 24 '22 13:10

slfan