Inject IHttpContextAccessor to CatalogContext's constructor will be destoryed when RevokeAuthenticationEvents.cs handle about CatalogContext's data

Author: CheunglikCreated Sep 9, 2023Updated Sep 9, 2023

if I only inject IHttpContextAccessor In CatalogContext's constructor. It' works! public CatalogContext(DbContextOptions options, IHttpContextAccessor httpContextAccessor) : base(options) { var name = httpContextAccessor.HttpContext?.User.Claims.SingleOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value ?? ""; } I can get the user's ID in the context's consturctor if user logged on. https://github.com/Cheunglik/eShopOnWeb/commit/5ca53d7675bc98baf0ff9c5e1d27c16e16f922ea withUserClaims

But I add some's data handling like ICatalogItemViewModelService in RevokeAuthenticationEvents public RevokeAuthenticationEvents(IMemoryCache cache, ILogger logger, ICatalogItemViewModelService catalogItemViewModelService) { _cache = cache; _logger = logger; _catalogItemViewModelService = catalogItemViewModelService; } https://github.com/Cheunglik/eShopOnWeb/commit/5ca53d7675bc98baf0ff9c5e1d27c16e16f922ea I can not get the user'ID in the context's consturctor even the user logged on. withoutUserClaims

I just fork from the original main braches https://github.com/dotnet-architecture/eShopOnWeb and add two simple comments. It 's so easy to reproduce it. In fact I want to handler some data in cookie's CookieAuthenticationEvents, I will add some tenant's records in user.claims. When user request controller => service=> repository => dbcontext. The dbContext can apply modelBuilder.Entity.HasQueryFilter(from userclaim's property) to isolate different tenant's record.

Please assist with the possibilities.

Source: dotnet-architecture/eShopOnWeb