我刚刚将我的代码从 Identity 1.0.0 完全迁移到 2.0.0 并且由于某种原因我遇到了一个奇怪的问题。在尝试创建用户时,它未能成功。错误是:电子邮件不能为空或为空。(当然,我会向该物业插入有效的电子邮件)。请帮忙..
控制器.cs
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser()
{
UserName = model.UserName,
contact_firstname = model.contact_firstname,
contact_lastname = model.contact_lastname,
Email = model.Email,
PhoneNumber = model.PhoneNumber,
contact_phone2 = model.contact_phone2,
street = model.street,
city = model.city,
country = model.country,
state = model.state,
postcode = model.postcode,
longitude = model.longitude,
latitude = model.latitude
};
var result = await UserManager.CreateAsync(user, model.Password);
//HERE IS THE ERROR
if (result.Succeeded)
{
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
AccountViewModel.cs
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(8, ErrorMessage = "Must be between {2} and {1} characters long.", MinimumLength = 4)]
[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 contact_firstname { set; get; }
[Required]
[Display(Name = "Last Name")]
public string contact_lastname { set; get; }
[Required]
[Phone]
[Display(Name = "Phone Number")]
public string PhoneNumber { set; get; }
[Phone]
[Display(Name = "Additonal Phone")]
public string contact_phone2 { set; get; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Country")]
public string country { set; get; }
[Display(Name = "State")]
public string state { set; get; }
[Required]
[Display(Name = "City")]
public string city { set; get; }
[Required]
[Display(Name = "Home Address")]
public string street { set; get; }
[Required]
[Display(Name = "Postal / Zip Code")]
public string postcode { set; get; }
public double longitude { set; get; }
public double latitude { set; get; }
}