6

In Asp.Net Core MVC 1.0 (MVC 6 RC1) the session timeout period is specified when session support is added in the ConfigureServices method in Startup.cs like so:

services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(30);
            options.CookieName = "Session";
        });

My question is this: elsewhere in the application how can this IdleTimeout value be accessed?

I would expect to find it as a property off of the HttpContext.Session object but it does not appear to be there. I've searched high and low and this doesn't seem to be documented anywhere. Thoughts?

RonC
  • 31,330
  • 19
  • 94
  • 139

1 Answers1

3

This appears to be a private field inside of the DistributedSession class. So at the moment, you can't get it. Session seems to be missing a lot of properties from the old HttpSessionState class, but I can't find anything pointing to what the reason may be (there might be a good one!).

You could get it with reflection, but it's pretty gnarly. The example below uses this answer.

public IActionResult Index()
{
    var value = GetInstanceField(typeof(DistributedSession), HttpContext.Session, "_idleTimeout");
    return View();
}

I would recommend storing the value in a configuration file or an internal class somewhere.

Community
  • 1
  • 1
Will Ray
  • 10,621
  • 3
  • 46
  • 61
  • 2
    +1 for recommending storing the value elsewhere and using it to set the `options.IdleTimeout`. This wasn't the way I wanted to solve the problem but if it's the only alternative to reflecting a private member var than it's the better of two hacks. – RonC May 09 '16 at 21:55