Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to access asp.net session variables through static properties of a static object?

Is it safe to access asp.net session variables through static properties of a static object?

Here is what I mean:

public static class SessionHelper
{
    public static int Age
    {
        get
        {
            return (int)HttpContext.Current.Session["Age"];
        }

        set
        {
            HttpContext.Current.Session["Age"] = value;
        }
    }


    public static string Name
    {
        get
        {
            return (string)HttpContext.Current.Session["Name"];
        }

        set
        {
            HttpContext.Current.Session["Name"] = value;
        }
    }
}

Is it possible that userA could access userB's session data this way?

like image 410
Ronnie Overby Avatar asked May 10 '10 16:05

Ronnie Overby


People also ask

Are asp net Session variables secure?

Very safe, . NET session variables are not the same as cookie variables which can be viewed from the client side, Session variables in this instance are only accessible from the C# code.

What's the main problem using Session variables in asp net?

The SessionID(eg: 11112222) that this client brings is different. We think that the browser of that client does not accept the SessionID. And finally, we abandon the use of Session, and solved this problem.

Is it good to use static variables in C#?

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.

Are static variables shared between sessions?

Yes, static values will remain same for all users. if one user is updating that value, then it will be reflected to other users as well.


2 Answers

Yes, that way is fine - just make sure you don't do this:

public static class SessionHelper {      private static HttpSession sess = HttpContext.Current.Session;     public static int Age     {         get         {             return (int)sess["Age"];         }          set         {             sess["Age"] = value;         }     } } 

As ive seen this way show one user's session data to another user. (Albeit in ASP.NET 1.1)

like image 176
Jamiec Avatar answered Oct 13 '22 23:10

Jamiec


IMHO, this is actually a good approach. It is type safe, add that level abstraction that could allow you to change things with minimal impact.

An example of something you might change, if you decided some state should move to the cache or even the database combined with caching, these would require additional thread synchronization, but could all be handled by the internals of this class. You might consider changing name of the class to something less session specific.

The one comment I would have on your particular example is that you should check that the Session variable is not null and either return an appropriate default, assert or raise an informative exception if it is. Just in case the property is read before it is being set.

like image 39
Chris Taylor Avatar answered Oct 14 '22 01:10

Chris Taylor