Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I am using MVC4, in my web application i am setting Status flag to true in database for logged in user's. when user's Log out, i am setting status to false. I am saving userId in Session , based on that userId i am doing this.
but when session Expired , my userId id gone. now how can we do this ?
I need to set status flag false, when Session Expires and browser closes.
How can we do this ?
Posted

1.For session expiration, you could do this by managing Session_End event in MvcApplication class from your Global.asax.cs, like in the next example:
C#
protected void Session_End(object sender, EventArgs e)
       {
           SiteSession siteSession = (Session["SiteSession"] == null ? null : (SiteSession)Session["SiteSession"]);
           using (MvcBasicSiteEntities db = new MvcBasicSiteEntities())
           {
               //Update the visitor log entry!
               VisitorLog.OnUserLogoff(db, (siteSession == null ? 0 : siteSession.VisitorLogID), true);
           }
       }

2.For browser closing you should do it by using JavaScript and AJAX call from your layout (_Layout.cshtml) like in the next example:
JavaScript
<script type="text/javascript">
        
        function onWindowClosing() {
            if (window.event.clientX < 0 || window.event.clientY < 0) {
                $.ajax({
                    type: "POST",
                    url: "/Account/OnWindowClosing"
                });
            }
        };

        function onKeydown(evt) {
            if (evt != undefined && evt.altKey && evt.keyCode == 115) //Alt + F4 
            {
                $.ajax({
                    type: "POST",
                    url: "/Account/OnWindowClosing"
                });
            }
        };

        window.onbeforeunload = onWindowClosing;
        window.document.onkeydown = onKeydown; 
       
    </script>

And in the controller the action for managing the browser closing will invoke the same method from business logic for updating the visitors log.
C#
public void OnWindowClosing()
        {
            SiteSession siteSession = this.CurrentSiteSession;
            VisitorLog.OnUserLogoff(_db, (siteSession == null ? 0 : siteSession.VisitorLogID), false);
            //
            Session.Clear(); // This is like a user log off!
        }

3.For details and source code about all of these you could use my next article: MVC Basic Site: Step 4 – jqGrid Integration in MVC 4.0 using AJAX, JSON, jQuery, LINQ, and Serialization[^]
 
Share this answer
 
Comments
dorababu407 9-Sep-14 8:05am    
Thank you. works fine
Raul Iloc 9-Sep-14 8:39am    
I am glad that I could help you!
dorababu407 9-Sep-14 13:34pm    
one issue.
i used your code without if condition [if (window.event.clientX < 0||window.event.clientY < 0)], works fine. by adding if condition not working, condition is not satisfying. what is that
window.event.clientX ,window.event.clientX
Raul Iloc 10-Sep-14 1:52am    
That condition is to manage the case when the user close the browser by using X button from the upper corner of the browser window!
dorababu407 10-Sep-14 3:00am    
yes , i thought the same , it is for checking closing the browser using X button.
but it is not working , i checked in chrome, firefox and IE10,11.
solution from below link works for me except in firefox

How to end user session when browser closed[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900