title: How to extend IdentityUser as a claim in ASP.NET Core / MVC 6 / EF7?
body: <p>I am building a site that has Users that belong to an Account. The account is identified by an AccountId which is a foreign key for most data in the DB such as Charges (associated to an Account) or Receipts (associated to an Account).</p>
  
  <p>Rather than hitting the DB every time the repository needs to be polled for data to get the user's AccountId, I wanted to add the AccountId as a claim.  The goal being to do something like:</p>
  
  <pre><code>_repository.GetAllChargesByAccountId(User.Identity.GetAccountId());
  </code></pre>
  
  <p>I'm finding only tidbits and partial solutions for this and I haven't been able to resolve some differences between those examples and my specific environment (ASP.NET Core RC1, MVC 6, EF7).</p>
  
  <p>I have derived a class from IdentityUser for adding attributes about the user:</p>
  
  <pre><code>public class UserIdentity : IdentityUser {
      public static object Identity { get; internal set; }
      public int AccountId { get; set; }
  }
  </code></pre>
  
  <p>I have created a UserIdentityContext that derives from IdentityDbContext that I'm using for my EF user store.</p>
  
  <p>And I have the following AuthController:</p>
  
  <pre><code>public class AuthController : Controller {
      private SignInManager&lt;UserIdentity&gt; _signInManager;
  
      public AuthController(SignInManager&lt;UserIdentity&gt; signInManager) {
          _signInManager = signInManager;
      }
  
      public IActionResult Login() {
          if (User.Identity.IsAuthenticated)
              return RedirectToAction("Dashboard", "App");
  
          return View();
      }
  
      [HttpPost]
      public async Task&lt;ActionResult&gt; Login(LoginViewModel vm, string returnUrl) {
          if (ModelState.IsValid) {
              var signInResult = await _signInManager.PasswordSignInAsync(vm.Username, vm.Password, true, false);
              if (signInResult.Succeeded) {
                  if (String.IsNullOrWhiteSpace(returnUrl))
                      return RedirectToAction("Dashboard", "App");
                  else return RedirectToAction(returnUrl);
              } else {
                  ModelState.AddModelError("", "Username or password is incorrect.");
              }
          }   
  
          return View();
      }
  
      public async Task&lt;IActionResult&gt; Logout() {
          if (User.Identity.IsAuthenticated) {
              await _signInManager.SignOutAsync();
          }
  
          return RedirectToAction("Index", "App");
      }
  }
  </code></pre>
  
  <p>Looking at other posts, it sounds like I need to add an IdentityExtension in order to access the claim as User.Identity.GetAccountId() and generate a custom user identity as in this answer: <a href="https://stackoverflow.com/questions/28335353/how-to-extend-available-properties-of-user-identity">How to extend available properties of User.Identity</a> but obviously this is done in an older version and many of the method calls are not applicable anymore.</p>
  
  <p>Thanks in advance for any answers or guidance.</p>