title: How to add claim to user dynamically?
body: <p>I want to save some small info (say, user's device id) about the user, after or during sign-in. This data is dynamic, and I'd like the lookup to be very fast (without database hit), as I need it in many controllers. I tried adding it to user's claims like this:</p>
  
  <p><code>((ClaimsIdentity)User.Identity).AddClaim(new Claim("UserDeviceId", userDeviceId));</code></p>
  
  <p>to subsequently retrieve it like this: <code>User.FindFirstValue("UserDeviceId")</code> inside any controller. But it seems that the data is not persisted between requests! It returns NULL on another HTTP request.</p>
  
  <p>I use standard SingInManager.PasswordSignIn() to sign the user in. I don't have claim store at all - I don't use default Identity EF implementation, I supply my own stores (IUserStore, IUserPasswordStore, and IUserRoleStore implementations). But even if I did, this data is dynamic and should not be persisted to / retrieved from a storage at all. The data is available upon sign in (basically client app sends hardware id).</p>
  
  <p>As I understand, the dynamically added claim gets wiped out because it is never injected into the cookie, when I add it to <code>User.Identity</code> it just adds to in-memory instance, which gets overwritten by the cookie at next request. (or at least that's my best guess).</p>
  
  <p>Is there any solution to this problem? I'd really like to avoid setting up Session mechanism, all I need is a <em>single</em> small value persisted across the requests, and even though strictly speaking it might not fully qualify as a "user claim", it is (conceptually) very close - it is a device id the user is currently connected from.</p>
  
  <p>I use <code>User.FindFirstValue(ClaimTypes.NameIdentifier)</code> in my controllers to get userId without any performance impact, and it is <em>very</em> convenient. It's setup by Identity itself obviously. I'd like to have similar mechanism for another identifier, preferably without rewriting half of Asp.Net Identity :) Is that doable? If not, what's my best alternative (besides setting up Session storage or supplying the value in every request)?</p>