Share via


LogOff ASP.NET Identity on Session Expires

Question

Friday, December 18, 2015 1:28 PM

Hi, 

Is there any simple way to force a LogOff from an MVC application using ASP.NET Identity, on the event Session_OnEnd?

Thanks

All replies (4)

Wednesday, January 6, 2016 2:18 PM âś…Answered

Hi, 

What I ended up doing was to add in the controllers the following test:

            // control the session timeout
            if (Session["name"] == null) {
                return RedirectToAction("Login", "Account");
            }

and added LogOff() to make sure that before Login there is no user logged in:

        public ActionResult Login(string returnUrl)
        {
            LogOff();
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

and it is working fine.

Thanks.


Monday, December 21, 2015 2:57 AM

Hi santiago17,

Is there any simple way to force a LogOff from an MVC application using ASP.NET Identity, on the event Session_OnEnd?

I'm not sure why you need to force the current user to log off when a session expires, in my experience, when a session expires and refresh the page, the user need to login the website again. But for your problem, i find a similar thread, you could refer to the following link and check if it's helpful to you.

http://stackoverflow.com/questions/26182660/how-to-logout-user-in-owin-asp-net-mvc5

Best Regards,

Klein zhang


Monday, December 21, 2015 11:53 AM

Hi, 

thank you for your reply. It is because I use several Session context values along the application and when the session expires I want to force a new login so that those variables are again instantiated instead of getting an error for having those values set to null after the session expires. 

Thanks


Tuesday, December 22, 2015 7:11 AM

Hi santiago17,

because I use several Session context values along the application and when the session expires I want to force a new login so that those variables are again instantiated instead of getting an error for having those values set to null after the session expires

According to your description, you'd like to handle Session timeouts in ASP.NET MVC instead of getting an error, isn't it? In my experience, you could consider to create a couple of action filters to provide cross-cutting checks for timeout scenarios.

http://markfreedman.com/handling-session-and-authentication-timeouts-in-asp-net-mvc/

Best Regards,

Klein zhang