gpt4 book ai didi

c# - asp.net identity userName 是唯一的吗?

转载 作者:可可西里 更新时间:2023-11-01 08:42:29 25 4
gpt4 key购买 nike

我正在阅读 Microsoft 中的用户身份并尝试将它们应用到我的 MVC5 应用中。

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

我尝试进行用户名验证,但我做不到。

这是数据库定义:

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

这是 IdentityUser 定义,注意(无验证):

namespace Microsoft.AspNet.Identity.EntityFramework
{
public class IdentityUser : IUser
{
public IdentityUser();
public IdentityUser(string userName);

public virtual ICollection<IdentityUserClaim> Claims { get; }
public virtual string Id { get; set; }
public virtual ICollection<IdentityUserLogin> Logins { get; }
public virtual string PasswordHash { get; set; }
public virtual ICollection<IdentityUserRole> Roles { get; }
public virtual string SecurityStamp { get; set; }
public virtual string UserName { get; set; }
}
}

在注册时,调用 UserManager.CreateAsync 方法,定义如下:

     public async Task<ActionResult> 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<IdentityResult> CreateAsync(TUser user, string password);

我在代码中的任何地方都没有看到验证,但是它不允许您输入现有的用户名。

我认为了解这是如何工作的将提高我对 asp.net 的身份概念的体验,并将改进我的代码。

非常感谢任何指导

最佳答案

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

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

如果您不想要这种行为,您可以直接从 DbContext 继承。

关于c# - asp.net identity userName 是唯一的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com