电子邮件不能为空或空。 MVC 5,Identity 2.0.0

时间:2014-05-08 09:01:28

标签: asp.net-mvc-5 asp.net-identity-2

我刚刚将我的代码从Identity 1.0.0完全迁移到2.0.0&由于某种原因,我遇到了一个奇怪的问题。 在尝试创建用户时,它无法成功。 错误是:电子邮件不能为空或空。 (当然,我当然会向酒店插入有效的电子邮件)。 请帮助..

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; }               
}

2 个答案:

答案 0 :(得分:1)

AccountViewModel.cs 中,将[EmailAddress]更改为[DataType(DataType.EmailAddress, ErrorMessage = "E-mail is not valid")]

答案 1 :(得分:1)

您必须从RegisterViewModel中删除UsernameEmail,因为它们是在 IdentityUser 类中定义的,我认为也是PhoneNumber。

确认在创建用户的public async Task<ActionResult> Register(RegisterViewModel model)中按CONTROL + SPACE:

var user = new ApplicationUser()
{ //Here...