public static class HttpContextAccessorMiddleWare { public static IServiceCollection AddHttpContextAccessor(this IServiceCollection services) { if(services == null) { throw new ArgumentNullException(nameof(services)); } services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); return services; } }
I then added it in the Startup.cs class as
// middleware services.AddHttpContextAccessor();
I then did dependency injection on a health controller
private readonly IHealthSvc _health; private readonly IHttpContextAccessor _httpContextAccessor; public HealthController(IHealthSvc health, IHttpContextAccessor httpContextAccessor) { _health = health; _httpContextAccessor = httpContextAccessor; }
When I try to do a poke, I wanted to see what the value was, and I keep getting null
public IActionResult Poke() { var y = _httpContextAccessor.HttpContext.User.Identity.Name; // I keep getting null here. I don't know why
I keep getting a null value and I do not know why.
Has anyone faced something like this before?