title: ASP.NET MVC Custom user fields on every page
body: <p><strong>Background:</strong>
  I'm building more and more web applications where the designers / template makers decide that adding a "profile picture" and some other user-related data, of course only when someone is logged in.</p>
  
  <p>As most ASP.NET MVC developers I use viewmodels to provide razor layouts with the information that I need shown, sourced from repositories et al. </p>
  
  <p>It is easy to show a user name through using</p>
  
  <pre><code>HttpContext.Current.User.Identity.Name
  </code></pre>
  
  <p>What if I want to show information that's saved in my backing datastore on these pages? Custom fields in the ApplicationUser class like a business unit name or a profile picture CDN url. </p>
  
  <p><em>(for sake of simplicity let's assume I use the Identity Framework with a Entity Framework (SQL database) containing my ApplicationUsers)</em></p>
  
  <p><strong>Question</strong></p>
  
  <p>How do you solve this:</p>
  
  <ol>
  <li>Without poluting the viewmodel/controller tree (e.g. building a BaseViewModel or BaseController populating / providing this information?</li>
  <li>Without having to roundtrip the database every page request for these details?</li>
  <li>Without querying the database if a user is not logged in?</li>
  <li>When you cannot use SESSION data (as my applications are often scaled on multiple Azure instances - <a href="https://www.simple-talk.com/cloud/platform-as-a-service/managing-session-state-in-windows-azure-what-are-the-options/" rel="nofollow">read why this isn't possible here</a>- I'm not interested in SQL caching or Redis caching.</li>
  </ol>
  
  <p>I've thought about using partials that new their own viewmodel - but that would still roundtrip the SQL database every pageload. Session data would be safe for now, but when scaled up in azure this isn't a way either. Any idea what would be my best bet?</p>
  
  <p><strong>TLDR;</strong></p>
  
  <p>I want to show user profile information (ApplicationUser) on every page of my application if users are logged in (anon access = allowed). How do I show this info without querying the database every page request? How do I do this without the Session class? How do I do this without building base classes?</p>