1. Principle In global.asax in .net, there are Application_AuthenticateRequest events and Application_BeginRequest events that are triggered every time the aspx file is accessed. However, Application_BeginRequest cannot identify identity tickets that have undergone FROMS authentication. So it can only be placed in Application_AuthenticateRequest.
My implementation principle is: every time the aspx file is accessed, it will be judged whether there is this user in the online table (the user name has been logged in, the IP address has not been logged in), if it does not exist, the user's identity, and finally The access time, last accessed IP, and last accessed URL are stored in the database. If it is already in the database, update the record and update the last access time, IP and last access URL.
At the same time, delete data in the database that is more than 20 minutes apart from the current time (no operation for 20 minutes is considered a timeout).
2. Advantages: You can not only see the accurate number of people currently online, but also know who is online, whether they are logged in, the proportion of visitors who are members, and their location, and calculate the number of people on a certain page. .
3. Database structure:
Primary key field type length whether it is empty or not
1uson_serialint40 serial number
0uson_uservarchar200 username (IP if not logged in)
0uson_companyvarchar1000 company name (if not logged in, it is 'visitor')
0uson_ip varchar200IP address
0uson_datedatetime80 last operation time
0uson_urlvarchar1000 last operation page path
4. Program notes:
1. The program is located in global.asax
2. I am using FORMS authentication.
3. Please use System.Web.Security
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string strUserID = string.Empty;
string strCompany = string.Empty;
if (Request.IsAuthenticated)
{
FormsIdentity identity = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = identity.Ticket;
strUserID = User.Identity.Name;
strCompany = ticket.UserData.Split("|".ToCharArray())[2];
}
else
{
strUserID = Request.UserHostAddress;
strCompany = "Visitor";
}
MemberOnlineInfo objOnline = new MemberOnlineInfo(strUserID, Request.UserHostAddress, DateTime.Now.ToString(), Request.FilePath, strCompany);
MemberAccount account = new MemberAccount();
if (!account.CheckUserOnline(strUserID))
account.AddOnline(objOnline);
else
account.UpdateOnline(objOnline);
//Delete timed out members
account.DeleteOnline();
}