【问题标题】:Don't make username unique asp.net identity不要让用户名唯一的 asp.net 身份
【发布时间】:2018-05-25 15:28:48
【问题描述】:

我想为用户名验证实现自定义逻辑。为用户名自定义验证创建了函数ValidateEntity,但如果我在创建用户时提供唯一的用户名,则ValidateEntity 函数被命中,如果我提供重复的用户名,则此函数不会被命中。

IdentityModel.cs

    public class ApplicationUser : IdentityUser
        {
            public int AppId { get; set; }
            //other attributes
            public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
            {
                var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                return userIdentity;
            }
        }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
        {
            public ApplicationDbContext()
                : base("DefaultConnection", throwIfV1Schema: false)
            {
            }

            public static ApplicationDbContext Create()
            {
                return new ApplicationDbContext();
            }

            protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
            {
                if ((entityEntry != null) && (entityEntry.State == EntityState.Added))
                {
                    var user = entityEntry.Entity as ApplicationUser;
                    //custom logic for username validation
                }
                return base.ValidateEntity(entityEntry, items);
            }
        }

AccountController.cs

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password); //shouldn't it always goto ValidateEntity function?
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }
            return View(model);
        }

更新: 我添加了public new string UserName { get; set; },现在我收到错误Name cannot be null or empty 这是数据的屏幕截图。

【问题讨论】:

  • 如果您还没有,不妨看看this question的答案
  • 谢谢,我已经尝试过这个解决方案,很遗憾没有成功。
  • 你找到答案了吗?

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


【解决方案1】:

用户名字段不唯一是不好的做法,除非您打算在自定义验证器中涵盖该字段。但是,您应该能够覆盖 ApplicationUser 类中的用户名字段:

public class ApplicationUser : IdentityUser
{
    public int AppId { get; set; }

    // Override username field
    public new string UserName { get; set; }

    //other attributes
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

此外,如果您的数据库已经存在,那么您还必须记住删除唯一索引 UserNameIndex 才能正常工作。

【讨论】:

  • 我收到错误:Name cannot be null or empty 请查看更新后的问题
猜你喜欢
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多