Home

Thursday, 14 July 2016

Know How many user access application using C#(Anonymous User)

Step 1) Write Front end code for user interaction where display user count.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ApplicationExample.WebForm1" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <!-- display no of user access application -->
    </div>
    </form>
</body>
</html>
Step 2) Add below code as a back end in class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ApplicationExample
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("No Of User Online " +Application["OnlineUser"]);
        }
    }
}
 Step 3) Write below code in Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace ApplicationExample
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Application["OnlineUser"] = 0;
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();
            Application["OnlineUser"] = (int)Application["OnlineUser"] + 1;
            Application.UnLock();
        }
        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["OnlineUser"] = (int)Application["OnlineUser"] - 1;
            Application.UnLock();     
        }
    }
}

2 comments: