简体   繁体   中英

How to check for session null on master page without logging out

I have a problem with reading value from session on every page load. I have to check if this session value is null on master page because I need to show the user real name when it's logged in so i can't rely only on forms authentication.

I'm also doing the same thing on every page load.

if (Session["Nome"] == null)
{
    FormsAuthentication.RedirectToLoginPage();
}

I'm not sure why or if it's due to slow loading but even when i'm still authenticated i get redirected to login so at some point the session is null. I was wondering if is there a better way to show user name(not username) on master page and redirect to login page when the variable is actually null.

Well by default Session variables have a time span of 20 minutes, so if you are not refreshing the variable it will be null after the time span passes. So you either have to reset the time span for the "Nome" variable ( not recommended as staying on one page for 21 minutes would force the user to re login after another postback ) or check

if(!HttpContext.Current.User.Identity.IsAuthenticated)
    FormsAuthentification.RedirectToLoginPage();
var username = HttpContext.Current.User.Identity.Name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM