I've just made a full migration of my code from Identity 1.0.0 to 2.0.0 & for some reason i encounter a weird problem. While trying to create the user, it fails to succeed. The error is: Email cannot be null or empty. (although, of course i insert valid email to the property). Please Help..
Controller.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; }
}
From your AccountViewModel.cs, change your [EmailAddress]
to [DataType(DataType.EmailAddress, ErrorMessage = "E-mail is not valid")]
You have to delete Username
and Email
from RegisterViewModel because they are defined in IdentityUser class I think also PhoneNumber.
confirms pressing CONTROL + SPACE in public async Task<ActionResult> Register(RegisterViewModel model)
where you are creating User:
var user = new ApplicationUser()
{ //Here...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With