Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Error when try to add new fields in ASP.NET Identity

$
0
0

Hi Guys,

in my ASP.NET MVC5 application, I'm customizing profile information in ASP.NET Identity by following the tutorial written by Pranav Rastogi. According to this I modified:

1) Models\IdentityModels,cs by adding three new properties:

    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 MiddleName { get; set; }
        public string LastName { get; set; }
    }

2) Models\AccountViewModels.cs by adding these three new fields

    public class RegisterViewModel
    {
        /*--------------------------------------------- Added by Phil 2015-03-12 /*---------------------------------------------*/
        [Required(ErrorMessage = "First name is required")]
        [StringLength(20, ErrorMessage="The first name must be shorter than 20 characters.")]
        [Display(Name="First name")]
        public string FirstName { get; set; }

        [StringLength(20, ErrorMessage = "The middle name must be shorter than 20 characters.")]
        [Display(Name = "Middle name")]
        public string MiddleName { get; set; }

        [Required(ErrorMessage = "Last name is required")]
        [StringLength(50, ErrorMessage = "The last name must be shorter than 50 characters.")]
        [Display(Name = "Middle name")]
        public string LastName { get; set; }
        /*------------------------------------------------------------------------------------------------------------------------------------*/

        [Required(ErrorMessage = "Email is required")]
        [EmailAddress(ErrorMessage = "This is not a valid email address")]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Password is required")]
        //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [StringLength(100, ErrorMessage = "The password must be at least {2} characters long.", MinimumLength = 8)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

3) Controllers\AccountController.cs, Register action, in order to store those new fields

        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, UserName = model.Email, Email = model.Email};
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)

However, after rebuild application and despite running migration and update of the db, an error occur when try to register a new user:

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269). 
  Descrizione: Eccezione non gestita durante l'esecuzione della richiesta Web corrente. Per ulteriori informazioni sull'errore e sul suo punto di origine nel codice, vedere la traccia dello stack. 

 Dettagli eccezione: System.InvalidOperationException: The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

Errore nel codice sorgente: 



Riga 153:            {
Riga 154:                var user = new ApplicationUser { FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, UserName = model.Email, Email = model.Email};
Riga 155:                var result = await UserManager.CreateAsync(user, model.Password);
Riga 156:                if (result.Succeeded)
Riga 157:                {
 

Thanks in advance for your help.

Phil


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>