I would like to flush a user's session with my website as soon as they close their web browser. How do I go about implementing that?
Make an ajax call at onbeforeunload() event and call :
Session.Abandon();
This is not easy as http is stateless. So you don't have an open connection which is closed when the browser closes. No way is guaranteed to work. A connection could be dropped, or power go down which would make any 'onClose' event unreliable.
You can time out sessions after a certain amount of inactivity. However there is no guarantee that a user hasn't just gone to lunch and wants an open session when they return.
If you are worried about indicating people as offline you could do some polling in ajax to keep sessions alive and destroy sessions that have not been used in longer than the poll interval. But if you are just worried about resources I would guess that this will do more harm than good.
You have no event on server-side triggered when user closes window or leaves the page, you can use the timeout of sessions the check it, and then, ensure the session does't dies on an idle user, that could be at the bathroom, or making coffe, validating it with AJAX or so.
MakeDummyRequest(); // with a simple http request, you can keep your session alive.
As other indicated your best bet is probally to make an Ajax call which will keep the session alive. This call can occur at fast interval if you keep it lean. Then you can use some sort of session timeout algorithim.
With that said there a couple of things to note. You will probally want to build a way in the protocol to force a user to logout of the application. Imagine that a user is logged in to your application and he goes home for the weekend without shutting down his computer.
Over the weekend you do a deploy which will invalidate all the ajax communication becuase you modified the protocol or something. Unless you force him out he will continue to be logged in and could be getting errors when he comes in Monday morning.