Desidero sviluppare un'applicazione in MVC utilizzando Identity Framework 1.0 che consente agli utenti di registrarsi con lo stesso nome utente utilizzato da un altro utente.Come consentire all'utente di registrarsi con Nome utente duplicato utilizzando Identity Framework 1.0
Quando si elimina un utente, desidero impostare la proprietà personalizzata IsDeleted
su true anziché eliminare l'utente dal database. In questo caso un altro utente può utilizzare UserName
dell'utente il cui IsDeleted
è impostato su true.
Ma il metodo predefinito UserManager.CreateAsync(user, password);
impedisce di farlo.
avevo ignorato il metodo di IdentityDbContext
ValidateEntity
come questo
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
if ((entityEntry != null) && (entityEntry.State == EntityState.Added))
{
ApplicationUser user = entityEntry.Entity as ApplicationUser;
if ((user != null) && this.Users.Any<ApplicationUser>(u => string.Equals(u.UserName, user.UserName) && u.IsDeleted==false))
{
return new DbEntityValidationResult(entityEntry, new List<DbValidationError>()) {
ValidationErrors = {
new DbValidationError("User", string.Format(CultureInfo.CurrentCulture,
"", 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,
"", new object[] { role.Name })) } };
}
}
return base.ValidateEntity(entityEntry, items);
}
Ecco il mio metodo di registro dove utente viene creato
metodopublic async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email.ToLower(), CreatedBy = model.UserName, CreatedDate = DateTime.UtcNow, };
user.ConfirmedEmail = false;
var result = await _accountService.CreateAsync(user, model.Password);
if (result.Succeeded)
{
TempData["MessageConfirm"] = user.Email;
return RedirectToAction("Confirm", "Account");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
ValidateEntity
dovrebbe essere eseguito quando await _accountService.CreateAsync(user, model.Password);
esegue. Ma è in esecuzione dopo che il metodo register
ha completato la sua esecuzione. Quindi il risultato genera errore.
Qualche suggerimento su come posso raggiungere questo obiettivo?
I nomi utente ** devono ** essere unici! Che problema stai cercando di risolvere! –
Non voglio che il nome utente sia univoco – Twix
Poiché il nome utente è l'equivalente di una chiave primaria ed è l'unico identificatore per un utente, ** deve ** essere univoco. Non hai scelta. In quale altro modo ti distinguerai tra gli utenti. Quindi, qual è il problema di base che stai cercando di risolvere? –