17

I changed over to use the new ASP.NET Identity 2. I'm actually using the Microsoft ASP.NET Identity Samples 2.0.0-beta2.

Can anyone tell me where and how I can modify the code so that it stores a user First and Last name along with the user details. Would this now be part of a claim and if so how could I add it ?

I assume I would need to add this here which is the register method in the account controller:

        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                ViewBag.Link = callbackUrl;
                return View("DisplayEmail");
            }
            AddErrors(result);
        }

Also if I did add the first and last names then where is this stored in the database? Do I need to create an additional column in a table for this information?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150

2 Answers2

18

You'll need to add it to your ApplicationUser class so if you use Identity Samples, I imagine you have something like that in your IdentityModels.cs

public class ApplicationUser : IdentityUser {
    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
        return userIdentity;
    }
}

after adding First and Last Names it would look like this:

public class ApplicationUser : IdentityUser {
    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
        return userIdentity;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Then when you register user, you need to add them to the list now that they are defined in ApplicationUser class

var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = "Jack", LastName = "Daniels" };

first and last names will end up in AspNetUsers table after you do the migrations

dima
  • 1,181
  • 1
  • 9
  • 20
  • Is your method the latest way of doing it ? I searched around and found this question: http://stackoverflow.com/questions/21404935/mvc-5-access-claims-identity-user-data Also I see some other examples of adding things as claims now. Have you looked into the claims way of doing it ? –  Apr 22 '14 at 06:11
  • yes, what you're asking for has nothing to do with claims... read [this](http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx).. there is no reason to overcomplicate things – dima Apr 22 '14 at 06:16
  • How do you get this to show up in the identity as a claim? Do I need another process to add it to the claims tables for that user? – Sinaesthetic May 21 '15 at 02:24
  • @dima I tried to follow your suggestion in my own project but `FirstName` and `LastName` are not ending up in my database after migrations and I am getting a `500 Internal Server Error` when trying to register a user with a first name and last name. [I made a post about it here](http://stackoverflow.com/questions/31034931/http-post-resulting-in-500-internal-server-error-with-c-sharp-mvc-after-addin). Please let me know if you have any ideas as to what might be causing this. Thanks. @Melina or anyone else, feel free to chime in. Thank you for your time. – user95227 Jun 24 '15 at 19:13
  • -1. @Melina I believe claims is the correct way to store this data. It might seem odd not normalizing the data (which is why dima probably didn't like the idea), but it does seem like claims is exactly for this purpose. See http://stackoverflow.com/questions/21645323/what-is-the-claims-in-asp-net-identity and http://stackoverflow.com/questions/21362751/user-identity-name-full-name-mvc5. The latter is answered by someone from the ASP.NET Identity team at Microsoft. – Justin Helgerson Mar 09 '17 at 01:56
  • Please take out the word "obviously". If it was obvious I wouldn't be here. – Matt May 24 '17 at 22:49
15

I realize this post is a few years old, but with ASP.NET Core gaining traction I ended up here having a similar question. The accepted answer recommends you update your user data model to capture this data. I don't think that it's a bad recommendation, but from my research claims is the correct way to store this data. See What is the claims in ASP .NET Identity and User.Identity.Name full name mvc5. The latter is answered by someone from the ASP.NET Identity team at Microsoft.

Here is a simple code sample showing how you add those claims using ASP.NET Identity:

var claimsToAdd = new List<Claim>() {
    new Claim(ClaimTypes.GivenName, firstName),
    new Claim(ClaimTypes.Surname, lastName)
};

var addClaimsResult = await _userManager.AddClaimsAsync(user, claimsToAdd);
Community
  • 1
  • 1
Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124