title: MVC6 CORE identity claims in razor view
body: <p>I am fiddling around with identity and I am definitely struggling - I have searched and searched, but nothing shows. </p>
  
  <p>I want to add claims to identity and then be able to post these in the view on my page. </p>
  
  <pre><code> public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
      {
          var authenticationType = "Basic";
          var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType);
  
          // Add custom user claims here
          userIdentity.AddClaim(new Claim("FirstName", this.FirstName));
  
          return userIdentity;
      }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Address { get; set; }
  </code></pre>
  
  <p>In my register user i save these with the user:</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Register(RegisterViewModel model)
      {
          if (ModelState.IsValid)
          {
              var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName};
              var result = await _userManager.CreateAsync(user, model.Password);
              if (result.Succeeded)
              {
                  // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                  // Send an email with this link
                  //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                  //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                  //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                  //    "Please confirm your account by clicking this link: &lt;a href=\"" + callbackUrl + "\"&gt;link&lt;/a&gt;");
                  await _signInManager.SignInAsync(user, isPersistent: false);
                  _logger.LogInformation(3, "User created a new account with password.");
                  return RedirectToAction(nameof(HomeController.Index), "Home");
              }
              AddErrors(result);
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>
  
  <p>Note, that i checked my database and FirstName and LastName are being stored! </p>
  
  <p>I then extended my identity <em>(Note i removed the original return, to check if it indeed returned null)</em></p>
  
  <pre><code> public static class IdentityExtension
  {
      public static string GetFirstName(this IIdentity identity)
      {
          var claim = ((ClaimsIdentity)identity).FindFirst("FirstName");
          // Test for null to avoid issues during local testing
          // return (claim != null) ? claim.Value : string.Empty;
          return claim.ToString() ;
      }
  
  
  }
  </code></pre>
  
  <p>I now figured I should be able to access the data from my razor view, which is in <strong>_Navigation.cshtml</strong></p>
  
  <pre><code>@if (Context.User.Identity.IsAuthenticated)
              {
                  &lt;div class="dropdown profile-element"&gt;
                      &lt;a data-toggle="dropdown" class="dropdown-toggle" href="#"&gt;
                          &lt;span class="clear"&gt;
                              &lt;span class="block m-t-xs"&gt;
                                  &lt;strong class="font-bold"&gt;Hello @User.Identity.GetFirstName()&lt;/strong&gt;
                              &lt;/span&gt;
  </code></pre>
  
  <p>I also attempted to do this without the extension, directly:</p>
  
  <pre><code> @{
   if (Context.User.Identity.IsAuthenticated)
   {
       var FirstName = ((ClaimsIdentity)User.Identity).FindFirst("FirstName");
   } 
  }
  </code></pre>
  
  <p>I am obviously missing something.. I can access the regular username without a problem. I have searched and found a lot to even get here, but a lot of it is different versions, so im stuck..</p>
  
  <p>I'd appreciate if you can help me out!</p>
  
  <p>Thanks.</p>