Monday, February 10, 2020

Custom Registration In ASP.NET MVC

1.Add new properties
click Ctrl + F and find "ApplicationUser : IdentityUser"

public class ApplicationUser : IdentityUser
    {
        //New properties
        public string FirstName { get; set; }

        public string LastName { get; set; }
        // existing code
        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;
        }
    }
2.Find out "public class RegisterViewModel"
Add new properties in this model
        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

3.find out "public async Task<ActionResult> Register(RegisterViewModel model)"
Add properties in

var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
FirstName = model.FirstName, //new property
LastName = model.LastName    //new property
};

and build application

4. Goto Tools > NuGet Package Manager > Click "Package Manager Console"
Now add the following commands in Console Manager
  1. Enable-Migrations

  2. Add-Migration UpdateTable

  3. Update-Database –verbose

Build and Run the application