asp.net身份userName是唯一的吗?

我正在阅读微软的用户身份,并试图在我的MVC5应用程序中应用它们。

据我所知,Id是关键,而userName不是键,定义说它可以为null,所以我问自己……为什么在MVC5项目模板中,当你输入一个已经存在的userName时你会收到错误信息??

我试图达到userNamevalidation,但我不能。

这是数据库定义:

CREATE TABLE [dbo].[AspNetUsers] ( [Id] NVARCHAR (128) NOT NULL, [UserName] NVARCHAR (MAX) NULL, 

这是IdentityUser定义,通知(无validation):

 namespace Microsoft.AspNet.Identity.EntityFramework { public class IdentityUser : IUser { public IdentityUser(); public IdentityUser(string userName); public virtual ICollection Claims { get; } public virtual string Id { get; set; } public virtual ICollection Logins { get; } public virtual string PasswordHash { get; set; } public virtual ICollection Roles { get; } public virtual string SecurityStamp { get; set; } public virtual string UserName { get; set; } } } 

在注册时,调用UserManager.CreateAsync方法,这里是定义:

  public async Task Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.UserName }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); } 

这是关于CreateAsync的最后一件事:

 public virtual Task CreateAsync(TUser user, string password); 

我在代码中的任何地方都没有看到validation,但它不允许您输入现有的userName。

我认为理解这是如何工作将改善我使用asp.net的Identity概念的经验,并将改进我的代码。

任何指导都非常感谢

这发生在IdentityDbContext 中,您的ApplicationDbContext可能inheritance自。 它会覆盖DbContext的ValidateEntity方法来进行检查。 看到这个反编译的代码:

  protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary items) { if ((entityEntry != null) && (entityEntry.State == EntityState.Added)) { TUser user = entityEntry.Entity as TUser; if ((user != null) && this.Users.Any(u => string.Equals(u.UserName, user.UserName))) { return new DbEntityValidationResult(entityEntry, new List()) { ValidationErrors = { new DbValidationError("User", string.Format(CultureInfo.CurrentCulture, IdentityResources.DuplicateUserName, new object[] { user.UserName })) } }; } IdentityRole role = entityEntry.Entity as IdentityRole; if ((role != null) && this.Roles.Any(r => string.Equals(r.Name, role.Name))) { return new DbEntityValidationResult(entityEntry, new List()) { ValidationErrors = { new DbValidationError("Role", string.Format(CultureInfo.CurrentCulture, IdentityResources.RoleAlreadyExists, new object[] { role.Name })) } }; } } return base.ValidateEntity(entityEntry, items); } 

如果您不想要此行为,则可以直接从DbContextinheritance。

当我查看ASP.NET身份的示例( https://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples )时,我注意到他们使用的UserValidator默认设置为RequireUniqueEmail = true;

该示例使用以下代码将RequireUniqueEmail属性设置为true。

 public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore(context.Get())); // Configure validation logic for usernames manager.UserValidator = new UserValidator(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; return manager; } 

也许这就是用户名在MVC应用程序中是唯一的原因。 尝试将属性设置为false!?