title: Razor MVC, where to put global variables that's accessible across master page, partiview and view?
body: <p>Hello Razor MVC Gurus: </p>
  
  <p><strong>Newbie question.</strong> </p>
  
  <p>Background. I have a custom IIdentity that is set in an HttpModule before it gets to controller &amp; views. To use it, i have to do</p>
  
  <pre><code>   MyIdentity myIdentity = (MyIdentity)((GenericPrincipal)context.User).Identity;
     MyComplexUser user = myIdentity.User;
     //user.name //user.location //user.username  //etc
  </code></pre>
  
  <p>The problem is, I use the object in different places such as </p>
  
  <ul>
  <li>master layout</li>
  <li>Some sub level nested layouts</li>
  <li>Some partialviews</li>
  <li>Some views </li>
  </ul>
  
  <p>It really depends on what properties of "MyComplexUser" object the views need. </p>
  
  <p>Currently, in the views, I have to do this really complicated casting to get to a property. For instance, if I want the "Name" of the user, I need to do </p>
  
  <p>@(((MyComplexUser)(((MyIdentity)((GenericPrincipal)context.User).Identity).User)).Name)</p>
  
  <p>I suppose I could put it in the controllers and then populate the ViewBag with a ViewBag.MyUser property, but then </p>
  
  <ol>
  <li>I don't like to use ViewBag. I prefer strongly typed objects</li>
  <li>If I use a strongly typed object ("MyUser") for views, then I have to popular all those models with a "MyUser" property. Feels a bit dirty? As I like to keep my models clean and be specific to the views they are involved with.  Besides, it gets unnecessarily repetitive. </li>
  <li>In places like master_layout.cshtml or partialviews, how do you access "MyUser"  if I put them in a controller?</li>
  <li>Use RenderAction and build partialviews for each User property is an overkill?</li>
  </ol>
  
  <p>Thanks. Again, I'm a newbie at MVC 4, any suggestion is greatly appreciate it. </p>