title: Is this technique of ASP.NET session access multi-user-safe?
body: <p>I am looking at a design pattern which has come up in quite a few of my firm's projects. It has historically functioned correctly, however I have heard some other developers argue that there is a possibility of session corruption using this pattern. I'm looking for insight from other .NET developers here on Stack Overflow. </p>
  
  <p>Basically, there's a class -- usually either <code>static</code> or a Singleton pattern, depending largely on the developer who wrote it -- stored in <code>App_Code</code>.</p>
  
  <p>This class encapsulates access to the current session via properties. All of these properties take the form of:</p>
  
  <pre><code>public static class SessionHelper
  {
      public static string SessionValue
      {
          get
          {
              object o = HttpContext.Current.Session["sessionValueName"];
              if (o == null)
              {
                  // Replace the following with code to store &amp; retrieve
                  // a default value of the appropriate datatype.
                  o = string.Empty;
                  HttpContext.Current.Session["sessionValueName"] = o;
              }
  
              return o.ToString(); // or cast, ensure cast is valid &amp; return.
          }
          set
          {
              HttpContext.Current.Session["sessionValueName"] = value;
          }
      }
  
      // Other properties, strongly-typed, as above.
  }
  </code></pre>
  
  <p>(These are not static when the class is a Singleton.)</p>
  
  <p>I have seen issues with static data on web sites in the past, largely because static data was used to maintain per-session state. (I have, for example, seen statically-declared "per-user" members of code-behinds. Not my code; my team was the clean-up crew for the company that wrote that mess.)</p>
  
  <p>However, because this is just a static entry to <code>HttpContext.Current.Session</code>, it seems like it should be safe, as it is not fundamentally any different than the <code>Page</code> class encapsulating this in the <code>Session</code> property. As I said, no other site on which my company has worked that used this pattern saw it ever have any issues -- and that includes some pretty large and highly active userbases. But I want to just get a fresh perspective.</p>
  
  <p>Are there potential multi-user issues, race conditions, or other failings/flaws which specifically could cause session corruption in the above pattern?</p>