【问题标题】:UserManager.AddPasswordAsync() returns "Name cannot be null or empty"UserManager.AddPasswordAsync() 返回“名称不能为空或为空”
【发布时间】:2014-12-29 21:37:08
【问题描述】:

我使用 MVC5 和 ASP.NET 身份框架。

我扩展了由 VS2013 搭建的帐户管理功能。

如果我的用户已向外部登录提供商注册并想要添加本地密码,我在调用“AddPasswordAsync()”时会收到错误:“名称不能为空或为空”

我查看了我的数据库并确认用户名不为空。

这个错误是什么意思?

我的 ApplicationUser 如下所示:

public class Person : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Sex Sex { get; set; }

    [Display(Name = "Birth date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Birthdate { get; set; }
    public string EMail { get; set; }
    public int PostalCode { get; set; }

    public Collection<Race> Races { get; set; }
    public Collection<Participation> Participations { get; set; }

    public Person()
    {
        if (string.IsNullOrEmpty(UserName))
        {
            UserName = EMail;
        }
        Participations = new Collection<Participation>();
        Races = new Collection<Race>();
        Birthdate = DateTime.Now;
    }
}

最好的问候 弗雷德里克

【问题讨论】:

  • 您的ApplicationUser 班级是什么样的?
  • 很抱歉我错过了这个。现在插入了
  • 你解决了这个问题吗?我收到“名称不能为空或为空”。错误也是如此。我也将 NAME 属性插入到我的视图模型中,但它仍然给我这个错误。如果你已经解决了,请通过发布它作为答案与我们分享。谢谢
  • 嗨阿米尔。唉,我还没有解决这个问题:(

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


【解决方案1】:

经过多次尝试,我终于解决了。错误消息是错误的,它来自数据库,意味着用户名字段为空,但数据库需要它。

确保向数据库发送用户名,例如:

 var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Hometown = model.Hometown };

并通过运行 update-database 确保数据库在此之前是最新的。

【讨论】:

    【解决方案2】:

    嗯,我希望您的 ApplicationUser 能提供更多关于问题所在的线索,但我没有看到任何突出的问题。所以,这就是我能给出的建议。不是在问UserName;它明确表示“名称是必需的”。 AddPasswordAsync 只是将用户对象保存到数据库中,因此您需要深入研究并确保 整个 对象是干净的,即没有验证错误。某处某处验证失败,所以我的第一步是运行一个项目,甚至在解决方案范围内搜索“名称”。不幸的是,您可能会因为这样的一般情况而得到大量的误报,但是当您最终找到具有该名称的一个或多个属性时,这很可能是您的罪魁祸首。

    【讨论】:

      【解决方案3】:

      您好,我尝试复制您的问题,但我认为错误不在您的 ApplicationUser 中。我认为问题出在您尝试创建用户时。因为在您的 Person 构造函数中,您将 UserName 分配为等于 EMail(我认为您不需要这些行)。但 Identity 在创建实例之前验证用户名。我做了和你一样的例子,这行我得到了同样的错误:

      //With out UserName property
      var usertemp = new ApplicationUser() {
          Profile_Id = "admin",
          Email = "rommelmeza@gmail.com",
          FirstName = "Rommel",
          LastName = "Meza",
          Password = model.Password,
          Active = true
      };
      
      var result = await UserManager.CreateAsync(usertemp, model.Password);
      // Error Name cannot be null or empty
      

      您需要将 UserName 属性显式添加到此:

      //With UserName property
      var usertemp = new ApplicationUser() {
          Profile_Id = "admin",
          UserName = "rommelmeza@gmail.com",
          Email = "rommelmeza@gmail.com",
          FirstName = "Rommel",
          LastName = "Meza",
          Password = model.Password,
          Active = true
      };
      
      var result = await UserManager.CreateAsync(usertemp, model.Password);
      // Success!!!
      

      它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多