5

I using asp.net identity. I create the default asp.net mvc application that implement user identity. The application use HttpContext.User.Identity to retrieve user id and user name :

string ID = HttpContext.User.Identity.GetUserId();
string Name = HttpContext.User.Identity.Name;

I am able to customize AspNetUsers table. I add some properties to this table but want to be able to retrieve these properties from HttpContext.User. Is that possible ? If it is possible, how can I do it ?

2

1 Answer 1

8

You can use Claims for this purpose. The default MVC application has a method on the class representing users in the system called GenerateUserIdentityAsync. Inside that method there is a comment saying // Add custom user claims here. You can add additional information about the user here.

For example, suppose you wanted to add a favourite colour. You can do this by

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
    // Add custom user claims here
    userIdentity.AddClaim(new Claim("favColour", "red"));
    return userIdentity;
}

Inside your controller you can access the claim data by casting User.Identity to ClaimsIdentity (which is in System.Security.Claims) as follows

public ActionResult Index()
{
    var FavouriteColour = "";
    var ClaimsIdentity = User.Identity as ClaimsIdentity;
    if (ClaimsIdentity != null)
    {
        var Claim = ClaimsIdentity.FindFirst("favColour");
        if (Claim != null && !String.IsNullOrEmpty(Claim.Value))
        {
            FavouriteColour = Claim.Value;
        }
    }

    // TODO: Do something with the value and pass to the view model...

    return View();
}

Claims are good because they are stored in cookies so once you've loaded and populated them once on the server, you don't need to hit the database again and again to get at the information.

4
  • Thanks for your answer. One problem that can't make your answer true is that I want to store dynamic data not static. For example, I want to store picture url for the current user. I can make a request to database to get picture url and store it in the claim if I can get user id. In the method public async Task<ClaimsIdentity> GenerateUserIdentityAsync , I can't access HttpContext.Current.User.Identity.GetUserId(). It is always null. Can you solve this ? Aug 17, 2015 at 11:31
  • 1
    I found a way to get user id (this.id). Thanks for your solution. Off course +1 Aug 17, 2015 at 11:40
  • 1
    Great, glad to hear that helped! I'll also point out that you are not restricted to just populating Claims only from inside the GenerateUserIdentityAsync method. You can add and remove claims in other parts of your code too. To do so, you create a new ClaimsIdentity(User.Identity), and then add or remove claims as needed.
    – spoida
    Aug 18, 2015 at 3:31
  • Thanks again. I appreciate your help. Aug 18, 2015 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.