Htttpcontext and claim not accessible in TenantInfoMiddleware

Viewed 21

I have a employee controller I have added two lines to get the employee TenantName one using httpcontext and a other using user claim. I want to get the TenantName in TenantInfoMiddleware

    [HttpPost, Route("login")]
    [AllowAnonymous]
    public async Task<IActionResult> Login([FromBody] LoginModel user)
    {
         Claim tenantName = new Claim("TenantName", "erp_colombia");
         HttpContext.Items["TenantName"] = "erp_colombia"; 
    }

However in my TenantInfoMiddleware both testHttpContext and testFromClaim are null why is that?

public class TenantInfoMiddleware
{
    private readonly RequestDelegate _next;

    public TenantInfoMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        List<Claim> claims = new List<Claim>();
        claims = context.User.Claims.ToList();
        var testHttpContext = context.Items["TenantName"];
        Claim claim = claims.Where(x => x.Type == "TenantName").FirstOrDefault();
        var testFromClaim = claim.Value;

        // Call the next delegate/middleware in the pipeline
        await _next(context);
    }
}

I have added the TenantInfoMiddleware in startup

  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    [Obsolete]
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseMiddleware<TenantInfoMiddleware>();
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
    }
}
0 Answers
Related