title: IHttpContextAccessor session invalid operation exception in CustomClaimsTransformer
body: <p>I followed the official docs to access the HttpContext from custom components in a CustomClaimsTransformer.</p>
  <p>I previously had a .NET Core web application, where I used the session to enable admins to jump into the application with the view (identity) of a different user (for support purposes). I store the information, for which user the view should be prepared in the Session. Now I wanted to make it more elegant and use the .net core authorization using claims and role based authorization. As there is a Windows authentication behind, I’ve to use the CustomClaimsTransformer. Now my problem is, I want to access the current session from within the CustomClaimsTransformer. I can inject the IHttpContextAccessor, but IHttpContextAccessor.Session always raises an invalid operation exception.</p>
  <p>Startup.cs:</p>
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
              .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
          options =&gt;
          {
              options.LoginPath = new PathString(&quot;/Account/Login/&quot;);
              options.AccessDeniedPath = new PathString(&quot;/Account/Forbidden/&quot;);
          });
          services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
          {
              options.CheckConsentNeeded = context =&gt; true;
              options.MinimumSameSitePolicy = SameSiteMode.None;
          });
          services.AddAuthorization(options =&gt;
          {
              options.AddPolicy(&quot;RequireAdministratorRole&quot;, policy =&gt; policy.RequireClaim(ClaimTypes.Role, &quot;admin&quot;));
              options.AddPolicy(&quot;Test1&quot;, policy =&gt; policy.RequireClaim(&quot;Rechte&quot;, &quot; Test1&quot;));
              options.AddPolicy(&quot;Test2&quot;, policy =&gt; policy.RequireClaim(&quot;Rechte&quot;, &quot; Test2&quot;));
  
          });
  
          services.AddMvc(config =&gt;
               {
                   var policy = new AuthorizationPolicyBuilder()
                      .RequireAuthenticatedUser()
                      .Build();
                   config.Filters.Add(new AuthorizeFilter(policy));
               }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
          //services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
          services.AddHttpContextAccessor();
          services.AddTransient&lt;IClaimsTransformation, CustomClaimsTransformer&gt;();
          services.AddDistributedMemoryCache();
          services.AddSession();
      }
  </code></pre>
  <p>CustomClaimsTransformer:</p>
  <pre><code>CustomClaimsTransformer:
  public class CustomClaimsTransformer : IClaimsTransformation
  {
      private readonly IHttpContextAccessor _httpContextAccessor;
      public CustomClaimsTransformer(IHttpContextAccessor httpContextAccessor)
      {
          _httpContextAccessor = httpContextAccessor;
      }
  
  
      public Task&lt;ClaimsPrincipal&gt; TransformAsync(ClaimsPrincipal principal)
      {
          var z = _httpContextAccessor.HttpContext; //works
          var zz = _httpContextAccessor.HttpContext.Session; // System.InvalidOperationException: &quot;Session has not been configured for this application or request.&quot;
  </code></pre>
  <p>I edited my ConfigureServices above, when pasting the code I removed some lines for readability, including the AddDistributedMemoryCache line, sorry. Session is working in the app except where shown.</p>
  <p>Configure:</p>
  <pre><code>        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          app.UseHttpsRedirection();
          app.UseStaticFiles();
          app.UseCookiePolicy();
          app.UseSession();
  
          app.UseAuthentication();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: &quot;default&quot;,
                  template: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
          });
      }
  </code></pre>