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 ?

Ahmed Shamel
  • 1,982
  • 3
  • 24
  • 58
  • You can implement your own IIdentity and IPrincipal classes, but you'll have to cast the HttpContext members to your type before you can access your extra properties. – Tieson T. Aug 16 '15 at 23:43
  • 2
    possible duplicate of [Can you extend HttpContext.Current.User.Identity properties](http://stackoverflow.com/questions/31974228/can-you-extend-httpcontext-current-user-identity-properties) – drneel Aug 17 '15 at 00:05

1 Answers1

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.

spoida
  • 2,655
  • 1
  • 23
  • 14
  • 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 GenerateUserIdentityAsync , I can't access HttpContext.Current.User.Identity.GetUserId(). It is always null. Can you solve this ? – Ahmed Shamel Aug 17 '15 at 11:31
  • 1
    I found a way to get user id (this.id). Thanks for your solution. Off course +1 – Ahmed Shamel Aug 17 '15 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 '15 at 03:31
  • Thanks again. I appreciate your help. – Ahmed Shamel Aug 18 '15 at 09:16