title: Azure B2C How to retrieve Built-In User Claims/Attributes
body: <p>I am attempting to retrieve some very basic information from Azure B2C, using the Built-In User Attributes and Claims.</p>
  
  <p>I merely want to return</p>
  
  <ol>
  <li>Given Name</li>
  <li>Surname  </li>
  <li>UserId</li>
  <li>Email</li>
  </ol>
  
  <p>Its not totally obvious (to me) how B2C is storing this content...
  The SignIn/SignOut Policy (User Attirubtes) displays Email Address as a string
  <a href="https://i.stack.imgur.com/Jl9fe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jl9fe.jpg" alt="Img 1"></a></p>
  
  <p>but the SignIn/SignOut Policy (Application Claims) displays Email Addresses as a stringCollection
  <a href="https://i.stack.imgur.com/jiNvA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jiNvA.jpg" alt="Img 2"></a></p>
  
  <p>Using the below code, I am attempting to return the 4 above Claims but only the </p>
  
  <ul>
  <li>UserId </li>
  <li>List item
  are coming through.
  <a href="https://i.stack.imgur.com/D8tT9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D8tT9.jpg" alt="enter image description here"></a></li>
  </ul>
  
  <p>I have used the JWT.IO to test the return Token and the Claims I'm looking for are there.</p>
  
  <p>Lastly, just to make things even stranger, MS seems to store my Email in a UserName field but doesn't show me an Email Field(s)? 
  <a href="https://i.stack.imgur.com/nK3Bc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nK3Bc.jpg" alt="enter image description here"></a></p>
  
  <p>I'm hoping to NOT have to make a seperate call to the Graph API in order to get these 2-3 fields I want.</p>
  
  <p>I'm just hoping someone can help me clarify where my code is going wrong.</p>
  
  <pre><code>    var claimsIdentity = (ClaimsIdentity)HttpContext.User.Identity;
      var userIdClaim = claimsIdentity.Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.NameIdentifier);
      if (userIdClaim != null)
      {
          userId = userIdClaim.Value;
          ViewData["userId"] = userId;
      }
      var GivenNameClaim = claimsIdentity.Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.GivenName);
      if (GivenNameClaim != null)
      {
          GivenName = GivenNameClaim.Value;
          ViewData["GivenName"] = GivenName;
      }
      var SurNameClaim = claimsIdentity.Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.Surname);
      if (SurName != null)
      {
          SurName = SurNameClaim.Value;
          ViewData["Surname"] = SurName;
      }
      var EmailClaim = claimsIdentity.Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.Email);
      if (Email != null)
      {
          Email = EmailClaim.Value;
          ViewData["Email"] = Email;
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Adding the below to my view helped..</p>
  
  <pre><code>  @foreach (Claim claim in User.Claims)
      {
       &lt;tr&gt;
       &lt;td&gt;@claim.Type @claim.Subject&lt;/td&gt;
       &lt;td&gt;@claim.Value&lt;/td&gt;
       &lt;/tr&gt;
      }
  </code></pre>
  
  <p>It returns</p>
  
  <ul>
  <li><a href="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" rel="nofollow noreferrer">http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier</a>
  6902e027-e475-447c-8f7d-75f4451f85a4  </li>
  <li><a href="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname" rel="nofollow noreferrer">http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname</a> Tim  </li>
  <li><a href="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname" rel="nofollow noreferrer">http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname</a> Cadieux</li>
  <li>emails me@email.com</li>
  </ul>
  
  <p>So I have updated me email to the belwo, which now works for 3/4 files, it does not return the collection of emails.</p>
  
  <pre><code>var Claims = User.Claims;
  var SurNameClaim = Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.Surname);
  ViewData["Surname"] = SurNameClaim.Value;
  
  var GivenNameClaim = Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.GivenName);
  ViewData["GivenName"] = GivenNameClaim.Value;
  
  var ClientIdClaim = Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.NameIdentifier);
  ViewData["ClientId"] = ClientIdClaim.Value;
  
  var EmailClaim = Claims.SingleOrDefault(c =&gt; c.Type == ClaimTypes.Email);
  if (EmailClaim != null)
  {
      ViewData["Email"] = EmailClaim.Value;
  }
  else
  {
      ViewData["Email"] = "Is Null";
  }
  </code></pre>