title: How to use sessions in a .net core 2.0 class library project
body: <p>We have a .net framework (4.5.1) asp.net website which authenticates users and and stores authentication details in a .net framework class library. This class library when referenced in a the target asp.net .net framework (4.5.x) website can access all the variables which were passed by the originating web site. </p>
  
  <p>We want to change the target applications to be in .net core 2.0 mvc. What I am trying to do is write a .net core (2.0) class library which will be referenced in the target app and consume the session variables data passed on by the originating app (which will still stay in .net framework 4.5.1) </p>
  
  <p>The original class library uses HttpContext.Current.Session which I now find is not used in .net core 2.0. All i want to do is have a .net 4.5 web app pass a session to a .net core 2.0 class library and assign it to a property which will be accessed by a .net core 2.0 mvc app.</p>
  
  <p>In the code below there is always a null exception. How do i acheive what i want to do or should i just use a .net framework mvc project as everything worjks fine in that. Cannot believe something as common as HttpContext.Current.Session is turning out to be such a nightmare. </p>
  
  <p>Thanks in advance for any help on this. </p>
  
  <p>`</p>
  
  <pre><code>using System;
  using System.Web;
  using System.Threading;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Http.Features;
  using System.Text;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.AspNetCore.Builder;
  
  namespace CLStandardCSharp1
  {
      //this is a .net core 2.0 class library project 
      public class HttpStateDataFactory
      {
          static byte[] toBytes = Encoding.ASCII.GetBytes("Some test value");
          static byte[] outToBytes;
  
          private static IHttpContextAccessor _httpContextAccessor;
          private static ISession _sessionContext =&gt; _httpContextAccessor.HttpContext.Session;
          public HttpStateDataFactory(IHttpContextAccessor httpContextAccessor)
          {
             _httpContextAccessor = httpContextAccessor;
          }
  
          public static void SetToSession()
          {
              _sessionContext.Set("SessionName", toBytes);
          }
  
          public static string GetFromSession()
          {
              string data = _sessionContext.TryGetValue("SessionName", out outToBytes).ToString();
              return data;
          }
  
          public static string SessionNameValueForClient = "N/A";
          public static string SomeProperty
          {
              get
              {
                  return SessionNameValueForClient= GetFromSession();
              }
              set
              {
                  SessionNameValueForClient = value;
              }
          }
      }
  }
  </code></pre>
  
  <p>`</p>