Jackal
Jackal

Reputation: 3521

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.

Upvotes: 1

Views: 509

Answers (1)

a.bajorinas
a.bajorinas

Reputation: 244

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;

Upvotes: 2

Related Questions