code_junkie

Limiting number of users accessing asp.net website

Nov 13th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. void Application_Start(object sender, EventArgs e)
  2. {
  3. Application["ActiveSessions"] = 0;
  4. }
  5.  
  6. void Session_Start(object sender, EventArgs e)
  7. {
  8. try
  9. {
  10. Application.Lock();
  11.  
  12. int activeSessions = (int) Application["ActiveSessions"] + 1;
  13. int allowedSessions = 10; // retrieve the threshold here instead
  14.  
  15. Application["ActiveSessions"] = activeSessions;
  16.  
  17. if (activeSessions > allowedSessions)
  18. System.Web.HttpContext.Current.Response.Redirect("~/UserLimitReached.aspx", false);
  19. }
  20. finally
  21. {
  22. Application.UnLock();
  23. }
  24. }
  25.  
  26. void Session_End(object sender, EventArgs e)
  27. {
  28. Application.Lock();
  29. Application["ActiveSessions"] = (int)Application["ActiveSessions"] - 1;
  30. Application.UnLock();
  31. }
Add Comment
Please, Sign In to add comment