0

I am trying to clear the session and the close the browser tab on logout link click. Please help me to do this.

Thanks in advance

  • 1
    For closing the browser, refer [this answer](http://stackoverflow.com/questions/853738/how-do-you-close-an-asp-net-mvc-page-from-the-controller). For clearing session, refer [this answer](http://stackoverflow.com/questions/1951784/logout-from-mvc) –  Aug 12 '15 at 04:13
  • @Stephen Muecke i tried that.. but its not closing – Arumuga Nainar P Aug 12 '15 at 06:41

4 Answers4

0

You can send an ajax request to to an action method which clears the session variables and in the call back, you can close the window.

<a id=logoutLink">Logout</a>

<script type="text/javascript">

$(function(){

  $("#logoutLink").click(function(e){
   e.preventDefault();
   $.post("@Url.Action("Logout","User")",function(res){
      if(res.status==="done")
      {
         //close the window now.
         window.close();
      }
   });
  });

});
</script>

Now in your UserController, Add the Logout action method

public class UserController : Controller
{
   [HttpPost]
   public ActionResult Logout()
   {
      Session.Abandon();
      return Json(new { status="done"});
   }
}

Reference: MVC for clossing browser tab when logout

0

You're trying to achieve two different things, one of which is a server-side concern, the other a client-side concern.

You could try splitting up these concerns, for example:

A: Using some Post/Redirect/Get laced with tab-closing javascript

  1. Post to Logout controller method
  2. Redirect to /LogoutSuccess
  3. Put some javascript that closes the browser tab on body load, in the LogoutSuccess view.

B: A more client-side approach

  1. Perform (in your javascript) a http GET to /Logout
  2. Upon 200 OK response, execute some javascript that closes the browser tab.
koenmetsu
  • 1,044
  • 9
  • 31
0
Session.Abandon();
or
Session.Clean();
or
Session[".."]=null;

close windows you can use js

windows.close();

in some browser,it will pop a confirm

chenZ
  • 920
  • 4
  • 16
0

This code is from MSDN. I have added the async : false to the jQuery call as that is the key to ensure that the call flushes the Server Side session and then the window closes.

<script type="text/javascript"> 
      
 $(document).ready(function () { 
  window.addEventListener('beforeunload',recordeCloseTime); 
 }); 
    
 function recordeCloseTime() { 
 
     $.ajax({ 
      type: "POST", 
            async: false,
         url: "ServiceToClearSession.asmx/RecordCloseTime", 
  });      
 } 

</script> 
Vishnoo Rath
  • 550
  • 7
  • 22