title: Retrieving/Reading Claim Values from ClaimsPrincipal
body: <p>If I get straight into it, I've built a <code>RESTful</code> Service (<code>WebAPI</code> V2) with <code>basic authentication</code>... All is working as expected but I'm very unsure on how to retrieve values from <code>ClaimsPrincipal</code>. I've read many articles but all point to using third party libraries And/or <code>Identity</code> in <code>.Net</code>.</p>
  
  <p>To keep it short and sweet, I have an <code>Attribute</code> performing necessary logic and a custom <code>authenticateService</code> which points to my <code>data store</code>.</p>
  
  <p>I have an <code>n-tier architecture</code>:</p>
  
  <ol>
  <li>API </li>
  <li>Service</li>
  <li>Business</li>
  <li>Data</li>
  </ol>
  
  <p>So I guess the first question is, how can I read the values from <code>ClaimsPrincipal</code>? (Apologies first time using Claims)</p>
  
  <p><strong>To Note: I'm expecting this to fire on each request, there will be no <code>session</code>.</strong></p>
  
  <p>Some logic that creates and authenticates the user (Inside <code>Attribute</code>)</p>
  
  <pre><code>using (var authService = new AuthenticateService())
              {
                  var client = await _authenticateService.AuthenticateAsync(
                      apiKey,
                      password);
  
                  if (client != null)
                  {
                      // Create a ClaimsIdentity with all the claims for this user.
                      Claim apiKeyClaim = new Claim("API Key", apiKey);
                      Claim clientNameClaim = new Claim(ClaimTypes.Name, client.ClientName);
                      Claim clientKeyClaim = new Claim("Client Key", client.ClientKey);
  
                      List&lt;Claim&gt; claims = new List&lt;Claim&gt;
                      {
                          apiKeyClaim,
                          clientNameClaim,
                          clientKeyClaim
                      };
  
                      // important to set the identity this way, otherwise IsAuthenticated will be false
                      // see: http://leastprivilege.com/2012/09/24/claimsidentity-isauthenticated-and-authenticationtype-in-net-4-5/
                      ClaimsIdentity identity = new ClaimsIdentity(claims, "Basic");
                      // AuthenticationTypes.Basic
  
                      var principal = new ClaimsPrincipal(identity);
                      return principal;
  
                      //var principal = new GenericPrincipal(new GenericIdentity("CustomIdentification"),
                      //                   new[] { "SystemUser" });
  
                      //return principal;
                  }
                  else
                  {
                      return null;
                  }
              }
  </code></pre>
  
  <p>Accessing Claim Values in my <code>API controller</code>:</p>
  
  <pre><code>[IdentityBasicAuthentication]
      [Authorize]
      [RoutePrefix("api")]
      public class OrderController : ApiController
      {
          private IOrderService _orderService;
          public OrderController(IOrderService orderService)
          {
              _orderService = orderService;
          }
          // POST api/&lt;controller&gt;
          [HttpPost]
          [Route("order")]
          public async Task&lt;IHttpActionResult&gt; Post([FromBody]Models.Model.Order order)
          {
  
              var modelResponse = new ModelResponse&lt;Models.Model.Order&gt;(order);
              if (order == null)
                  return BadRequest("Unusable resource.");
  
              if (!modelResponse.IsModelValid())
                  return this.PropertiesRequired(modelResponse.ModelErrors());
  
              try
              {
                  //Create abstracted Identity model to pass around layers
                  // Access Claim values here
                  //OR can I use Claims in other layers without creating an abstracted model to pass through.
                  await _orderService.AddAsync(order);
              }
              catch (System.Exception ex)
              {
                  return InternalServerError();
              }
              finally
              {
                  _orderService.Dispose();
              }
  
              return Ok("Order Successfully Processed.");
          }
      }
  </code></pre>
  
  <p>Really appreciate your time reading this, hopefully "someone" can direct/help me reading claim values and/or best approach to passing around layers.</p>
  
  <p>Regards,</p>