【问题标题】:Email cannot be null or empty. MVC 5, Identity 2.0.0电子邮件不能为空或为空。 MVC 5,身份 2.0.0
【发布时间】:2014-06-25 13:53:33
【问题描述】:

我刚刚将我的代码从 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; }               
}

【问题讨论】:

  • 我想,我的错误。在 IdentityModels.cs 中,我再次实现了 Email 属性。 --> 错误,因为 IdentityUser 已经有这样的属性。美好的一天,埃亚尔
  • 值得补充的是,它并不只是必须实现得到错误的Email 属性。我重新实现了Id,这也显示了Email cannot be null or empty 错误。所以检查 all 属性是否有重复!

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


【解决方案1】:

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

【讨论】:

  • 删除 IdentityModels.cs 中的 Email 属性解决了这个问题。
【解决方案2】:

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

确认在您正在创建用户的public async Task&lt;ActionResult&gt; Register(RegisterViewModel model) 中按 CONTROL + SPACE:

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

【讨论】:

    猜你喜欢
    • 2014-09-16
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2015-03-21
    • 2015-11-10
    • 1970-01-01
    相关资源
    最近更新 更多