title: How to get user id from IdentityServer in client app when including access token?
body: <p>I have implemented an authentication service based on IdentityServer3 and a simple MVC client app and a Shopper API secured by the authentication service. I've implemented a IdentityServer custom UserService so that the authentication service authenticates against our existing user data store. My Shopper API expects a userid in the Shopper Get request. Currently the response from authentication service includes the identity token and the access token, but no user id. I tried adding a user_id claim in the AuthenticationResult from my custom UserService.AuthenticateLocalAsync method, but I'm not seeing it in my client app code. 
  UserService.AuthenticateLocalAsync looks like this: </p>
  
  <pre><code>try
  {
      var user = new shopper(_dbConnLib, context.UserName, context.Password);
      var claims = new List&lt;Claim&gt; { new Claim("user_id", user.shopperid) };
      context.AuthenticateResult = new AuthenticateResult(user.shopperid, user.MemberDetail.billToAddress.FirstName, claims);
  }
  catch(shopperInitFromException ex)
  {
      context.AuthenticateResult = null;  // Indicates username/password failure
  }
  return Task.FromResult(0);
  </code></pre>
  
  <p>And my client app SecurityTokenValidated handler looks like this: </p>
  
  <pre><code>SecurityTokenValidated = async n =&gt;
  {
      var nid = new ClaimsIdentity(
          n.AuthenticationTicket.Identity.AuthenticationType,
          Constants.ClaimTypes.GivenName,
          Constants.ClaimTypes.Role);
  
      var userInfoClient = new UserInfoClient(
          new Uri(n.Options.Authority + "/connect/userinfo").ToString());
  
      var userInfo = await userInfoClient.GetAsync(n.ProtocolMessage.AccessToken);
      userInfo.Claims.ToList().ForEach(ui =&gt; nid.AddClaim(new Claim(ui.Type, ui.Value)));
  
      nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
  
      nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken));
  
      //nid.AddClaim(new Claim("user_id", n.ProtocolMessage.UserId));
  
      nid.AddClaim(new Claim("expires_at", DateTimeOffset.Now.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)).ToString()));
  
      n.AuthenticationTicket = new AuthenticationTicket(
          nid,
          n.AuthenticationTicket.Properties);
  }
  </code></pre>
  
  <p>If I step through that in the debugger, userInfo.Claims always has a count of 0. How can I get back a claim with the unique identifier of the user? Or can I get it from the identity or access token? Or should I just pass the tokens to the Shopper API and let it determine the id from the tokens? </p>