Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Session Timeout value in Asp.Net Core MVC 1.0 (aka MVC 6 RC1)?

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?

like image 779
RonC Avatar asked Oct 18 '22 10:10

RonC


1 Answers

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.

like image 116
Will Ray Avatar answered Oct 31 '22 22:10

Will Ray