Session C# how does it work?

Asked

Viewed 5,016 times

3

What is the lifetime of an ASP.NET MVC Session, and how to find out how to find out the remaining time and how to add more time to Session (if possible)?

There is another form of value guard that is more performative?

  • Man, aren’t you confusing Seth with cookies? because Session both in php and ASP.NET works the same way: The Session after it is created it generates a value for identifying each user and does not have time to expire only expires after user closes browser, when the user closes the browser destroys the active Session and after it access again another Session with different value is created.

2 answers

4


Possibly duplicated? (This link can help you too) What’s the difference between Sessions and Cookies?

When it comes to web, you can change the value of the timeout through the web.config:

<configuration>
  <system.web>
     <sessionState timeout="20"></sessionState>
  </system.web>
</configuration>

By default, the time is 20 minutes. As for getting the remaining time to finish Session, there are several ways, you would have to be more specific about this. One solution would be to control time via javascript. But if you want to, for example, redirect the user after Session is finished, you can use HttpContext.Current.User.Identity.IsAuthenticated and make such validation..

EDIT:

No, as far as I know, it is not possible to modify the session time at real time, since we are talking about downtime. You set the maximum downtime and, upon passage of time, the session is completed.

You can implement a solution to keep your session "alive" indefinitely as well, just "mix" an action in MVC and make the call via jQuery:

[HttpPost]
public JsonResult KeepSessionAlive() {
    return new JsonResult {Data = "Success"};
}
  • Um, my question is how can I know via code, how much time is left for a Session to expire, and if via code I can add more time to Session or simply renew its lifespan?

  • 1

    I edited the answer with some more information, see.

1

What would be a session of a system ?

Session is usually used to store user data. It is done this way because the data stays on the server, not allowing other users to have access.

You can save your application session in several ways:

Inproc: stores session status in Web server memory. This is the default.

Stateserver: stores the session status in a separate process called the ASP.NET status service. This ensures that the session status is preserved if the web application is restarted and also makes the session status available to multiple web servers on a web farm..

Sqlserver: stores the session status in an SQL Server database. This ensures that the session status is preserved if the web application is restarted and also makes the session status available to multiple web servers on a web farm..

Custom: allows you to specify a usual storage provider..

Off : No session.

https://msdn.microsoft.com/en-us/library/ms178586.aspx

Depending on your system you will need a different way.

Regarding the cycle that life, the user’s session is deleted when it loaps the system.

I would like to make a caveat, it is not necessary to use Session you can very well store the information in encrypted form in the browser cookie.

Follows exelende tutorial of Eduardo Pires MVP

http://eduardopires.net.br/2015/04/pense-duas-vezes-antes-de-utilizar-sessions/

Browser other questions tagged

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