[英]$http post resulting in 500 (Internal Server Error) with C# MVC after adding properties to built in User
嘗試使用ASP.net的內置用戶身份驗證注冊新用戶時,出現500 Internal Server錯誤。
這以前對我有用,但現在我嘗試將FirstName
和LastName
字段添加到“ AspNetUser”表中,並且收到500錯誤。
在我的app.js中,我正在使用angular與我的c Sharp控制器進行通信。 自從應用正確注冊用戶以來,我在添加的所有代碼旁邊添加了帶有“ **”的注釋。
以前,我僅使用email
, password
和confirmPassword
注冊用戶。 嘗試添加FirstName
和LastName
,出現500錯誤。
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
//[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
我試圖按照/ U /迪馬的意見, 從這個公認的答案 ,以及/ U /亞歷克斯Polyankin在我今天早些時候發布的問題通過添加FirstName
和LastName
屬性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
}
再次,我對自添加以來的所有代碼都進行了**評論。
調試時,出現用戶(具有名字和姓氏) 以使其正確進入控制器
但是, 這是我在調試器中所做的 。 在帳戶控制器中輸入最后一個if語句之前,我得到500錯誤。
這是調試器中的實際錯誤。
是什么導致此錯誤?
非常感謝您的寶貴時間。 讓我知道您是否需要任何其他信息,或者不清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.