I'm assuming you are using an SqlMembershipProvider. If so, this is the direction I would take...
Create a class that inherits SqlMembershipProvider and override the GetNumberOfUsersOnline() and ValidateUser() methods...
using System.Web.Security;
public class MyMembershipProvider : SqlMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
if (base.ValidateUser(username, password))
{
// successfully logged in. add logic to increment online user count.
return true;
}
return false;
}
public override int GetNumberOfUsersOnline()
{
// add logic to get online user count and return it.
}
}
Now if you're using a LoginStatus control to allow users to sign out, you can use the LoggedOut
event to add the decrement online user count logic there.
You will have to use your new custom membership provider in the web.config. For the type property of your membership, change it from whatever it says, something like type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
to something like type="MyNameSpace.MyMembershipProvider"
. I think that's all there is to it.
This solution allows you to keep using your SqlMembershipProvider provider with just a couple additions.