sta*_*low 11 c# asp.net validation asp.net-identity
我正在阅读微软的用户身份,并试图在我的MVC5应用程序中应用它们.
据我所知,Id是关键,而userName不是键,定义说它可以为null,所以我问自己......为什么在MVC5项目模板中,当你输入一个已经存在的userName时你会收到错误信息??
我试图达到userName验证,但我不能.
这是数据库定义:
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[UserName] NVARCHAR (MAX) NULL,
Run Code Online (Sandbox Code Playgroud)
这里是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; }
}
}
Run Code Online (Sandbox Code Playgroud)
在注册时,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);
}
Run Code Online (Sandbox Code Playgroud)
这是我达到的最后一件事CreateAsync:
public virtual Task<IdentityResult> CreateAsync(TUser user, string password);
Run Code Online (Sandbox Code Playgroud)
我在代码中的任何地方都没有看到验证,但它不允许您输入现有的userName.
我认为理解这是如何工作将改善我使用asp.net的Identity概念的经验,并将改进我的代码.
任何指导都非常感谢
Cas*_*sey 10
这发生在IdentityDbContext <TUser>中,您的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);
}
Run Code Online (Sandbox Code Playgroud)
如果您不想要此行为,则可以直接从DbContext继承.
当我查看 ASP.NET Identity ( https://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples )的示例时,我注意到他们使用默认设置为的 UserValidatorRequireUniqueEmail = true;
该示例使用以下代码将该RequireUniqueEmail属性设置为 true。
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
return manager;
}
Run Code Online (Sandbox Code Playgroud)
也许这就是用户名在您的 MVC 应用程序中是唯一的原因。尝试将属性设置为 false!?
| 归档时间: |
|
| 查看次数: |
13437 次 |
| 最近记录: |