7

I am using Session in .Net core, However i am able to set and get the Session data in Controller like

HttpContext.Session.SetString("User", "True");
var user = HttpContext.Session.GetString("User");

But when i am trying to use the same code in a concrete class i am not able to do so. It does not show GetString or SetString after HttpContext.Session.

It does not work after

HttpContext.Session

Please help Thanks

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
abhishek
  • 73
  • 1
  • 3
  • The context is held in the controller class only, if you need session info within a model class, pass the session object as a parameter of a method of your class via a controller. Otherwise your are kind of breaking the design pattern. – Nerevar May 23 '18 at 09:22

2 Answers2

8

To access session in non-controller class -

  1. First, register the following service in Startup.ConfigureServices

      services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
    
  2. Now, register a class (example - SessionManager) where you want to access the Session in Startup.ConfigureServices.

      services.AddScoped<SessionManager>(); 
    
  3. Now, in SessionManager class, add the following code.

      private readonly IHttpContextAccessor _httpContextAccessor;    
      private readonly ISession _session;    
      public SessionManager(IHttpContextAccessor httpContextAccessor)    
      {    
             _httpContextAccessor = httpContextAccessor;    
             _session = _httpContextAccessor.HttpContext.Session;    
       }  
    

The above code is receiving IHttpContextAccessor object through dependency injection and then, it is storing Sessions in a local variable.

shalitha senanayaka
  • 1,905
  • 20
  • 37
2

That's because HttpContext is a member of Controller, and outside a controller, it's a type name. See Access the current HttpContext in ASP.NET Core how to inject the IHttpContextAccessor into a class and access the session from there.

However, it's generally inadvisable to use the session in a class library. You'd better pass the particular values to your library call. So instead of accessing the settings in the library method, you do:

// controller code

var user = HttpContext.Session.GetString("User");
var libraryResult = _fooLibrary.Bar(user);
HttpContext.Session.SetString("UserResult", libraryResult);
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Actually i am trying to check the session in a custom ClaimRequirementFilter. So from a central location i want to check the session. I mean if the session is found then give the access to user else show unotherised page. – abhishek May 23 '18 at 09:31
  • Umm so why didn't you begin by explaining that? You can just [access `filterContext.HttpContext.Session` in `OnActionExecuting()`](https://stackoverflow.com/questions/32925219/how-to-create-a-custom-attribute-that-will-redirect-to-login-if-it-returns-false). – CodeCaster May 23 '18 at 09:33
  • 1
    Oh ! my :) that was so easy.. Thanks Man! – abhishek May 23 '18 at 09:38