I'm trying to determine the best way to handle current authentication for my app. There isn't any authentication by my app but will be handled from a login page before getting to my app. If they successfully authenticate my app will receive a cookie containing credentials and other user information.
What's the best way to determine if the cookie is present throughout the users session? I am currently reading the cookie on the startup page but this causes issues if the user bookmarks a page past that. Should I be checking on each page request is the cookie is there or can I check up front when the user hits the default page and store that somehow?
Here's how I'm currently grabbing the user from the cookie
UserId = _ltpaToken.LTPATokenParse();
if (UserId == "")
{
_logger.Error("No User found");
return RedirectToPage("/Error");
}
else
{
HttpContext.Session.SetString("UserId", UserId);
return RedirectToPage();
//user is good to
}
Then checking for the UserId again on another page
UserId = _httpContextAccessor.HttpContext.Session.GetString("UserId");
if(UserId == null)
{
Response.Redirect("ToCompanyLoginPage");
}
//continue on to page load
Is there a better way to do this?