Limiting the amount of concurrent logins per user is a typical practice in many real-world applications, including banking portals, trading platforms, admin panels, and business systems.
Data inconsistencies, license abuse, and security problems might result from permitting infinite logins with the same credentials.
This article describes a traditional concurrent login/session-count control requirement, explores several solutions, and offers a simple ASP.NET WebForms + SQL Server solution with functioning logic and output explanation.
Problem Statement
A single user account should be allowed to log in from only N sessions (example: 2, 3, or 4).
If the user logs in again after reaching the limit:
Either deny login OR
Automatically terminate the oldest inactive session and allow new login.
Sessions may remain active if:
Browser is closed suddenly
User forgets to log out
The system must handle these cases automatically.
Requirements Summary
Allow configurable session count per user
Track:
Login time
Last activity
Active / inactive status
Automatically expire unused sessions
Ensure latest active users are not disturbed
Possible Approaches
Option 1: ASP.NET Session Only (Not Recommended )
Uses
Session[]variablesFails when:
Browser crashes
Multiple devices used
No central control
Not suitable for concurrent login control
Option 2: Database-Based Session Tracking (Recommended )
Store sessions in a database table
Track activity per login
Kill old sessions safely
This is the correct and professional approach
Database Design
adminmaster Table
Stores user details and allowed session count.
UserSessions Table
Tracks each login session.
Concept Used
1. GUID-Based Session Identification
Each login creates a unique SessionId
Avoids conflicts
Safe across multiple users and devices
2. Least Recently Used (LRU) Strategy
Sessions ordered by
LastActivityOldest inactive session is terminated first
3. Configurable Session Limit
Each user has a different
SessionCountCan be changed without code modification
Login Flow Logic
Validate username & password
Read allowed session count
Fetch active sessions
If session limit reached:
Kill the oldest active session
Create a new session
Allow login
ASP.NET WebForms Login Code
Session Activity Update (BasePage Concept)
Auto-Expire Inactive Sessions (SQL Job)
Purpose
Handles browser crash
Cleans unused sessions
Keeps session count accurate
Output Explanation
Scenario 1
User allowed 3 sessions, logs in 3 times
All logins allowed
Scenario 2
User logs in 4th time
Oldest session is terminated automatically
New session is created
Login succeeds
Scenario 3
User closes browser without logout
Session becomes inactive after timeout
Slot is freed for new login
Secure
Scalable
Works across devices
Handles unexpected logouts
Professional enterprise-ready solution
Concurrent login control is a critical security feature in modern applications.
By
using a database-driven session tracking mechanism with GUID-based
session IDs and LRU session termination, we can efficiently control
active logins without impacting user experience.
This solution is simple, clean, and production-ready for ASP.NET WebForms applications.
Best ASP.NET Core 10.0 Hosting Recommendation
At HostForLIFE.eu, customers can also experience fast ASP.NET Core
hosting. The company invested a lot of money to ensure the best and fastest
performance of the datacenters, servers, network and other facilities. Its
datacenters are equipped with the top equipments like cooling system, fire
detection, high speed Internet connection, and so on. That is why
HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do
regular maintenance and monitoring works to assure its Orchard hosting are
security and always up.


0 comments:
Post a Comment