Clear session on Logout MVC 4

前端 未结 2 1229
面向向阳花
面向向阳花 2021-01-06 01:06

I need to know how to clear session on Logout in MVC 4 asp.net I have tried almost every thing but all in vain.

    [AllowAnon         


        
相关标签:
2条回答
  • 2021-01-06 01:27

    When you click on browser back button, it brings page from cache not from server. But user will not be able to perform any action on page displayed after back button.

    If you still want page should not be displayed you should remove cache. It's a best practice. Here is the code. (Write in Global.asax.cs file)

     protected void Application_BeginRequest()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
        }
    

    After writing this code user will not see previous page after pressing back button.

    The code written above to make Session object clear is right. No need to change any thing.

    0 讨论(0)
  • 2021-01-06 01:34

    When a user clicks the back button, the browser shows the cached version of the page, which is exactly what the user saw when he first downloaded that page. However, there is no contact with the sever at all to show this cached version of the page. The cached version of the page will look exactly as when it was downloaded, so you can see the user that was logged on when the page was downloaded. However, that doesn't mean that user is still logged in.

    If you want to avoid the user to navigate back to previous cached pages, the easier way to do it is to remove the browser's history when the user logs out. You cannot do directly that, but there is a workaround that gives the same result. Run this JavaScript:

    var Backlen=history.length;   history.go(-Backlen); 
    window.location.href='new page url';
    

    So, what you need to do is to return a page with this JavaScript, and, where you read 'new page url' you have to insert the page where you want to redirect the user when he logs out.

    Of course, avoiding caching, like in Ravinder Singh's answer is an option, but it's not a good idea to avoid the browser from caching all the pages.

    0 讨论(0)
提交回复
热议问题