In my ASP NET Core application, when processing a single request, a situation may arise when an ApplicationUser object (inheritor of IdentityUser) is required in several places, which encapsulates the properties of our authorized user.
Controller Code:
var user = await _userManager.GetUserAsync(HttpContext.User); //_userManager - объект UserManager<ApplicationUser> - AspNet Identity manageruser.NavProperty.Load(); //что-нибудь делаем с ним ... Next, ViewComponent. There we will count the number of unread messages:
public async Task<string> InvokeAsync() { var user = await _userManager.GetUserAsync(HttpContext.User); int messages = user.GetUnreadMessages(); ... } Actually, I am confused by the fact that we, for the same request, refer to the database several times to get the same object.
Where can I get it once so that it is available from anywhere in the application? In ViewData, it would have to be in every method of the controller. In Startup via DI, too, it will not work - its constructor will be called before dependencies are established.