简体   繁体   English

向内置用户添加属性后,$ http发布导致C#MVC导致500(内部服务器错误)

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

I'm getting a 500 Internal Server error when trying to register a new user with ASP.net's built in user authentication. 尝试使用ASP.net的内置用户身份验证注册新用户时,出现500 Internal Server错误。

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. 这以前对我有用,但现在我尝试将FirstNameLastName字段添加到“ AspNetUser”表中,并且收到500错误。

In my app.js I'm using angular to communicate with my c sharp controllers. 在我的app.js中,我正在使用angular与我的c Sharp控制器进行通信。 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 . 以前,我仅使用emailpasswordconfirmPassword注册用户。 When trying to add in the FirstName and LastName , I get the 500 error. 尝试添加FirstNameLastName ,出现500错误。

JS: 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: 'Joe@gmail.com',
    Password: 'Pass1234!',
    ConfirmPassword: 'Pass1234!',
    FirstName: 'Joe',       ///**added FirstName and LastName to test JS object
    LastName: 'Schmo'       ///**
}

AccountController 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 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 . 我试图按照/ U /迪马的意见, 从这个公认的答案 ,以及/ U /亚历克斯Polyankin在我今天早些时候发布的问题通过添加FirstNameLastName属性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 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. 在帐户控制器中输入最后一个if语句之前,我得到500错误。

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. 让我知道您是否需要任何其他信息,或者不清楚。

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 . 这里有更多示例信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM