I encountered this error, it did not provide any detail as to what the root cause was but I figured out what the problem is. I wanted to share it so that others that encounter it might have success in solving the problem.
I had the following class:
public class BankUser : IdentityUser, IUserProfile
{
#region IUserProfile Members
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Email Address")]
[EmailAddress(ErrorMessage="Invalid Email Address")]
public string Email { get; set; }
[Display(Name = "Time Zone")]
public int TimeZone { get; set; }
public Dictionary<string, string> TimeZoneOptions
{
get
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("(GMT -10:00) Hawaii", "-10");
d.Add("(GMT -9:00) Alaska", "-9");
d.Add("(GMT -8:00) Pacific Time", "-8");
d.Add("(GMT -7:00) Mountain Time", "-7");
d.Add("(GMT -6:00) Central Time", "-6");
d.Add("(GMT -5:00) Eastern Time", "-5");
d.Add("Unknown", "0");
return d;
}
}
[Display(Name = "Phone")]
[Phone(ErrorMessage = "Invalid phone number.")]
[StringLength(10, MinimumLength = 10, ErrorMessage = "Phone number must be 10 characters long.")]
public string Phone { get; set; }
#endregion
}
As you can see, I extended IdentityUser with additional properties. My view only had UserName and Password as form fields when trying to create a new user UserManager.CreateAsync()
. When trying to submit the form with just the UserName and Password the system would try to validate that Email was passed since it had the Required attribute.
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new BankUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Solution I must either add the Email as a field in my form or remove the Required attribute. Once I did either of them the code worked as expected.
Hope that helps.