Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does static reference to HttpContext.Current.Session return same session for all users?

Is there room for issue in the following code in terms of multiple users of the same web application? I mean, I know that a purely static string will be shared across all sessions for a single ASP.NET application, but given that this explicitly refers to the Current.Session, even though it is static it seems like it would always refer to the session instance of the "current user."

But an error is happening that could be explained by everyone sharing the current value of Mode and thus the most recent change overwriting everyone else's mode value.

(As a background: This string is in a Helpers class that is used throughout the application. I do not want to make hard-coded references to Session["Mode"] throughout the application and do not want to have to pass Session["Mode"] in every method call from an aspx.cs page.)

public static string Mode
{
    get
    {
        var value = HttpContext.Current.Session["Mode"];
        return (value ?? string.Empty).ToString();
    }
    set
    {
        HttpContext.Current.Session["Mode"] = value;
    }
}
like image 341
Devin Burke Avatar asked Feb 04 '11 15:02

Devin Burke


People also ask

What is the difference between session and HttpContext current session?

Current is null and HttpContext. Current. Session will throw null reference exception, but Page. Session will still be attached with page object so page method GetData can access the Page.

How does HttpContext current work?

The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

What is the difference between HttpContext current items and HttpContext current session in asp net?

Current. Item” data is live for single HTTP request/Response where HttpContext. Current. Session data is live throughout user's session.

Is session shared between users?

No, it is not shared.


1 Answers

HttpContext.Current always returns the context of the current request (if there is a current request).

Since each user will be executing a different request, each context will be different.

like image 105
John Saunders Avatar answered Sep 19 '22 05:09

John Saunders