12

I am implementing my own session provider, and would like to know if the default of 20 minutes is set in the session provider automatically? Is this value provided whether or not an entry is in the configuration file?

If not should my session provider is supposed to get it from another location?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • If you are implementing your own session provider, you should consider that you have to look in the configuration file to see if there are any settings for the default provider. However you should look in your own configuration (for you session provider) to find out the timeout for your implementation. – Peter Feb 06 '12 at 16:20
  • I considered that, but want to know if the 20 minutes is an implementation detail in the default session provider, or if it's available somewhere else. –  Feb 06 '12 at 16:29

5 Answers5

18

I couldn't find the value in a global configuration file, but it's defined somewhere.

Using the following code you can get the session timeout value whether or not it is defined in your local web.config.

Configuration conf = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
SessionStateSection section = (SessionStateSection) conf.GetSection("system.web/sessionState");
int timeout = (int) section.Timeout.TotalMinutes;
11

Try using:

System.Int64 timeout = System.Web.HttpContext.Current.Session.Timeout; // The time-out period, in minutes
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
Stephan Ahlf
  • 3,310
  • 5
  • 39
  • 68
  • 1
    this will throw null reference exception when you don't have a current Session started (e.g. during app start up) – hyankov Oct 28 '17 at 16:47
1

In Global.asax, you can set

Session.TimeOut

in Session_Start or set it in other place in the code.

antar
  • 510
  • 1
  • 3
  • 16
  • The question is about getting the value, and `HttpContext.Session` is null. –  Feb 06 '12 at 16:21
  • Did you try using HttpContext.Current.Session ? – antar Feb 06 '12 at 16:31
  • I actually had an instantiated `HttpContext` which represented the current context. `HttpContext.Current.Session` is therefore also `null`. –  Feb 06 '12 at 16:34
  • Please check this link: http://stackoverflow.com/questions/1382791/asp-net-what-to-do-if-current-session-is-null. Hope it helps. – antar Feb 06 '12 at 16:41
  • I'm pretty sure it's constructed **after** calling the session provider. –  Feb 06 '12 at 16:57
0

http://msdn.microsoft.com/en-us/library/aa478952.aspx

According to the article I've linked above, if the Timeout value is not set anywhere, a default of 20 minutes will be used. This seems to be the case for both custom providers and built-in.

Jeff Turner
  • 1,318
  • 8
  • 12
  • I read that too, but I need the current Timeout value, and `HttpContext.Session` is `null`. –  Feb 06 '12 at 16:15
  • http://stackoverflow.com/questions/1382791/asp-net-what-to-do-if-current-session-is-null if httpcontext.session is null then the session object isn't being initiated and there is no timeout and no other session related information. – Jeff Turner Feb 06 '12 at 16:21
  • I'm pretty sure it's constructed **after** calling the session provider. –  Feb 06 '12 at 16:57
0

You can just use Session.Timeout to get the value

G_P
  • 2,100
  • 3
  • 16
  • 18