Add username and last name in ASP.NET Identity 2?

I switched to using the new ASP.NET Identity 2. I really use Microsoft ASP.NET Identity Samples 2.0.0-beta2.

Can someone tell me where and how I can change the code so that it saves the username and last user along with the user data. Will this be part of the claim now, and if so, how can I add it?

I assume that I will 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 really added the first and last name, then where is it stored in the database? Do I need to create an extra column in a table for this information?

+7
c # asp.net-identity asp.net-identity-2
source share
2 answers

You need to add it to the ApplicationUser class, so if you are using identity samples, I assume that you have something like this 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 the first and last names it will 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 the user, you need to add them to the list now that they are defined in the ApplicationUser class

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

first name and last name end in the AspNetUsers table after migrations

+11
source share

I understand that this post is several years old, but with ASP.NET Core getting traction, I ended up here with a similar question. The accepted answer recommends updating the user data model to collect this data. I do not think this is a bad recommendation, but from my research applications - the right way to store this data. See What are the claims in ASP.NET Identity and User.Identity.Name mvc5 full name . The latter is answered by someone from the ASP.NET Identity team at Microsoft.

Here is a simple code example showing how you add these statements using the ASP.NET identifier:

 var claimsToAdd = new List<Claim>() { new Claim(ClaimTypes.GivenName, firstName), new Claim(ClaimTypes.Surname, lastName) }; var addClaimsResult = await _userManager.AddClaimsAsync(user, claimsToAdd); 
+3
source share

All Articles