3

I'm migrating a .NET 4 Framework ASP.NET MVC project to .NET Core and I have the following controller method (.NET 4):

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] // Never Cache
public virtual ActionResult Extend()
{
     // Re-establish the session timeout
     Session.Timeout = Utility.Constants.sessionTimout;
     return new EmptyResult();
}

This is my code in .NET Core

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
//[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] // Never Cache
public virtual ActionResult Extend()
{
    // Re-establish the session timeout
    // TODO: Need to find out in ASP.NET CORE
    
    //this.HttpContext.SessionSession.Timeout = Utility.Constants.sessionTimout;
    return new EmptyResult();
}

I have been trying to find out how to do Session.Timeout in .NET Core but haven't been lucky.

** UPDATE ** This method restarts timeout from an external application that is loaded into an IFrame. So when user is in the iFrame app, the main ASP.NET application is not getting any interaction at all. So that's why I require to extend the session timeout manually from the iFrame app.

Any clue?

7
  • Whats the error / behavior? Commented Apr 3, 2022 at 12:48
  • The above code is the .NET Framework Commented Apr 3, 2022 at 12:48
  • 1
    i don't get it... session timeout obviously is reset on any communication with the session owner, as the name implies... so any empty endpoint should do Commented Apr 3, 2022 at 12:51
  • and you'd initially configure the session timeout with builder.Services.AddSession Commented Apr 3, 2022 at 12:57
  • and sorry it's not any empty endpoint, since it actually needs to be called each time, it should have deactivated caching like in you sample or simply be a POST endpoint since they're not cached by default. Commented Apr 3, 2022 at 13:01

1 Answer 1

0

I am not familiar with the TimeOut property being available in .net core.

But, it is possible to use Session state as described here

You can set the timeout during setup:

builder.Services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromSeconds(10);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

A call to your Extend action should be sufficient to reset the idle timers.

Keep in mind, accessing the Session state is different in .net core, e.g.:

HttpContext.Session.SetString("foo", "bar");
string foo = HttpContext.Session.GetString("foo");

Typically, even within the IFrame, the using interacting with the page should be sufficient to reset the IdleTimeout.

As @Aria you can even detect the upcoming timeout and act accordingly.

Sign up to request clarification or add additional context in comments.

8 Comments

I just updated the post on why I need to restart session manually. Here is the explanation: ** UPDATE ** This method restarts timeout from an external application that is loaded into an IFrame. So when user is in the iFrame app, the main ASP.NET application is not getting any interaction at all. So that's why I require to extend the session timeout manually from the iFrame app.
@VAAA: but if the user is interacting with the IFrame, and the IFrame is calling Extend, the session idle timeout would be reset, right?
Mmm good question... I dont really know
Can you test it? Might be worth it :-)
@Aria: I reasoned something similar. I'll process your comment in the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.