1

In my web app, multiple users(more that 100) can login simultaneously. I need to maintain state of these users. If 100 users login at once, state of 100 users has to be maintained. I need UserID, Email and other properties during execution of code.

I had used static properties which is shown below in code. But this resulted to clashing of users.

I am thinking of using singleton pattern but it also has static property so I am not sure if it fulfills my need.

I also thought of using HttpContext.Current.Items but found out that it is per request. So, I think value added here will flush on every next request.

//code that clashed users

Public class UserHelper
{
    public static int UserID
    {
        get;
        set;
    }

    public static string Email
    {
        get;
        set;
    }
} 

What is the best practices to accomplish this task efficiently?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
CodesDDecodes
  • 132
  • 1
  • 15
  • Session Variables? - E.g. [Session variables in ASP.NET MVC](https://stackoverflow.com/questions/560084/session-variables-in-asp-net-mvc) – Alex K. May 30 '19 at 12:29
  • 1
    Static properties are a no-go in ASP.NET, since they are not thread-safe. – Heretic Monkey May 30 '19 at 12:30
  • Session variables or some other aproach like these: https://stackoverflow.com/questions/14820509/maintaining-state-in-asp-net-mvc-website – ibonetti May 30 '19 at 12:32
  • 1
    Static properties lives on Applcation memory, they're not thread safe and they're shared between sessions and requests... it's higly wrong to keep user info on properties shared with all users, in all their requests... a real unmanageable chaos! Static properties could be used for caching and info that are common to every users, managing the concurrency, but not for user info. Every user must have it's own non static object in Session that brings his info and that could be read only within his requests (except for CSRF successful attacks and other particular cases) – Sycraw May 30 '19 at 12:41
  • 1
    Remind, I kept it really simple, you shall learn an entire world on Application, session and request memories of a web application, the way that asp.net manage them with in different types of projects (webforms, MVC, Webapi...) and how you should use them. – Sycraw May 30 '19 at 12:43
  • Thanks everyone for your comment. My concern now is when I use session, is it safe? Session can be accesed from browser inspector.. also considering its impact on application performance.. – CodesDDecodes May 30 '19 at 15:41

0 Answers0