$http post resulting in 500 (Internal Server Error) with C# MVC after adding properties to built in User

3.3k views Asked by At

I'm getting a 500 Internal Server error when trying to register a new user with ASP.net's built in user authentication.

This was previously working for me, but now I've attempted to add a FirstName and LastName field to the 'AspNetUser' table and I'm receiving the 500 error.

In my app.js I'm using angular to communicate with my c sharp controllers. I've put comments with "**" next to all the code I added since the app was registering users properly.

Previously I only registered users with an email, password, and confirmPassword. When trying to add in the FirstName and LastName, I get the 500 error.

JS:

$http.post(API_END_POINT + '/api/Account/Register', user);  //**added to post the new user. this call is triggered when a header is clicked in my html

var user = {
    Email: '[email protected]',
    Password: 'Pass1234!',
    ConfirmPassword: 'Pass1234!',
    FirstName: 'Joe',       ///**added FirstName and LastName to test JS object
    LastName: 'Schmo'       ///**
}

AccountController

    //[Authorize]
[RoutePrefix("api/Account")]
public class AccountController : ApiController
{

        // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register/")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName}; //**added FirstName and LastName to ApplicationUser

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok();
    }

}

IdentityModels.cs

I tried to follow the advice of /u/dima from this accepted answer as well as /u/ Alex Polyankin in the question I posted earlier today by adding the FirstName and LastName properties to ApplicationUser.

    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
        // Add custom user claims here
        return userIdentity;
    }

    public string FirstName { get; set; }     //**added these per user Dima's suggestion
    public string LastName { get; set; }      //**

}

AccountBindingModels.cs

 public class RegisterBindingModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [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; }

    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }     //**added FirstName to  account binding class

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }       //**added FirstName to  account binding class

}

Again, I commented with ** on all the code I added since it was working.

When debugging, the user(with first and last name) appears to make it to the controller properly

However this is as far as I make it in the debugger. I get the 500 error before entering that last if statement in the account controller.

And here is the actual error in the debugger.

What might be causing this error?

Thank you very much for your time. Let me know if you need any additional information or if I am being unclear.

1

There are 1 answers

10
Alexander Polyankin On BEST ANSWER

From the comments above, it is clearly a db migration error. Code for package manager console:

enable-migrations
add-migration InitialCreate
update-database

More info with example here.