How to find out if the session has expired?

Asked

Viewed 1,470 times

2

I need to find out when the session expires, in ASP.Net MVC, so I can show a modal and redirect.

How can I do that?

There’s something in the httpcontext that I can use?

  • What do you want to do? show a modal for the user to say that he wants to stay logged in or display a modal saying that he has been undone by disuse?

  • Sorry, I don’t think I expressed myself right rs I want to show a modal pro user warning that he has been disused, then redirect him to login page.

2 answers

1

To check if the session is still valid just check the property: HttpContext.Current.User.Identity.IsAuthenticated.

And make the code you want according to the result. (modal, redirection)

I recommend you see the official documentation for more details and examples.

  • Mate, correct me if I’m wrong, but the property IsAuthenticated only checks if the user is authenticated, and not that the session is valid, or is different from null

  • Yes, @Thomaserichpimentel, isAuthenticated checks if you are authenticated by checking that user’s session is still valid. Session time should be set differently as Session.Timeout = 60; recommend you see the documentation regarding.

0

Since the server does not have a persistent connection to the client you can create a timer in javascript with the same duration of the Session on your server.

When you reach this time you display the modal and when you close the modal you can redirect it to the login screen. It doesn’t matter if you spent a few more seconds or not, as long as the server session no longer exists, it will be redirected, if you have made the correct validations...

In this case you have nothing to worry about, but if you still want to drop it, when you reach the time you can use an ajax call (post preference) in a simple action that deletes Session.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.