I have to get the user id from my database to do most of the functions in my application. I have .net core 2.1 application with windows authentication enabled.
My question is which is efficient way, is it to request User.Identity.Name
to check the loggeduser name and get the user id (with db query) or get the username from User.Identity.Name
for the first time, query the id and assign it to session and use the id to subsequent request.
User user = new User(xDbContext);
if ((HttpContext.Session.GetInt32("CurrentUserId") == null))
{
loggedUserId = userPrivilege.GetUserId(User.Identity.Name);
HttpContext.Session.SetInt32("CurrentUserId", loggedUserId);
}
else
{
loggedUserId = HttpContext.Session.GetInt32("CurrentUserId").Value;
}