Limiting number of users accessing asp.net website

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Limiting number of users accessing asp.net website on this date .

What is the best way to limit the number of (concurrent) users accessing a web application that any one can introduce for selling website/application to client and how to increase the number of users accessing it remotely?

Answer

If you use the in-process session state management, you can use the HttpApplicationState class, by introducing the Global.asax file and putting something like this in the code behind:

void Application_Start(object sender, EventArgs e)
{
    Application["ActiveSessions"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
    try
    {
        Application.Lock();

        int activeSessions = (int) Application["ActiveSessions"] + 1;
        int allowedSessions = 10; // retrieve the threshold here instead

        Application["ActiveSessions"] = activeSessions;

        if (activeSessions > allowedSessions)
            System.Web.HttpContext.Current.Response.Redirect("~/UserLimitReached.aspx", false);
    }
    finally
    {
        Application.UnLock();
    }
}

void Session_End(object sender, EventArgs e)
{
    Application.Lock();
    Application["ActiveSessions"] = (int)Application["ActiveSessions"] - 1;
    Application.UnLock();
}

Then, in the UserLimitReached.aspx you would call HttpSession.Abandon() to effectively terminate the current session so it does not count towards the limit. You’ll have to figure out the rest yourself. 🙂