A fundamental problem with web-sites is that if the user closes their browser the web-site will never know. The implications point to a better solution. You have to re-think what it means to be "logged in" to a web-site.
There is generally never a reason why you should need to know when a user "logs out" of our web-site, because there is no "logging in". Every time a user visits a page, the web-server authenticates who they are; whether this be through:
- NTML
- Kerberos
- HttpAuth
- a session cookie
- a persistent cookie
- some other mechanism (e.g. you IPv6 address)
The user "logs in" every page load. After the browser has fetched the page, the user no longer exist (as far as the web-server is concerned). This means the user "logs out" as soon as their page has been fetched.
But logging in is slow
The usual reason to have a special logon event is so that you can cache the user's information (e.g. name, permissions). Rather than having to do an expensive database fetch each time they simply refresh a page, why not fetch it from the cache? But, as a good developer, we also need to expire that information when it's no longer needed. But since there is no way to know when the user is "done" using your web-site, there is no way to know when to flush the cached information.
So don't cache it.
SQL Server has a very advanced set of data caching mechanisms; just query for the data again.
Another tempation might be to store the data in the user's Session
. That's perfectly valid; the information will remain there until the session ends. But a lot of people argue against storing data in a Session
, as that eats up memory. They argue instead to store it in a database like SQL Server. Which brings up back to: just query it again in SQL Server.
If fetching the information is expensive, or time consuming, then you can cache it - but use a cached that was designed to be a cache:
Page.Cache
which is an alias of
Page.Application.Cache