Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set session to the logged in user in ASP.NET MVC

I want to set session time for the user who sits idle without doing any work.

After a specific time when user comes again and works on that then it should redirect to login page.

like image 842
Jonathan Avatar asked Dec 11 '12 11:12

Jonathan


People also ask

How can manage user session in ASP NET MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same. But like session variable these elements cannot preserve values for all requests; value persistence varies depending the flow of request.

How can check user already login in ASP NET MVC?

You need to set FormsAuthentication. SetAuthCookie(PrimaryKey, false); when user is loggedIn. Here, PrimaryKey is the key that you can use throughout the session for identification of the user using User.Identity.Name . Also, when user log out of the application, you will call FormsAuthentication.

How session is implemented in MVC?

By default, Asp.Net MVC support session state. Session is used to store data values across requests. Whether you store some data values with in the session or not Asp.Net MVC must manage the session state for all the controllers in your application that is time consuming.


1 Answers

You need session timeout. There are different ways to do that using ASP.NET MVC. Please, take a look on these tutorials and choose the way that fits better to you:

  • Handling Session and Authentication Timeouts in ASP.NET MVC
  • Handling Session Timeout Gracefully
  • Detecting Session Timeouts using a ASP.Net MVC Action Filter
like image 79
Glauco Vinicius Avatar answered Sep 28 '22 08:09

Glauco Vinicius