Reputation: 1054
Im try to personalice strings Errors of asp.net-identity, this is my code:
private async Task< bool> addUser()
{
using (libProduccionDataBase.Contexto.DataBaseContexto context = new DataBaseContexto())
{
using (var t = new libProduccionDataBase.Identity.ApplicationUserManager( new libProduccionDataBase.Identity.ApplicationUserStore( context ) ))
{
ApplicationUser usr = new ApplicationUser
{
Nombre = AddNombreKTbox.Text,
ApellidoPaterno = AddApPaternoKTbox.Text,
ApellidoMaterno = AddApMaternoKTbox.Text,
ClaveTrabajador = AddClaveKTbox.Text,
UserName = AddLoginNameKTbox.Text,
Email = AddEmailKTbox.Text
};
IdentityResult result = await t.CreateAsync(usr, AddPasswordKTbox.Text);
if (result.Succeeded) t.AddToRole( usr.Id, "Usuario General" );
foreach(var err in result.Errors)
{
Console.WriteLine( err );
}
return result.Succeeded;
}
}
}
But i need the errors in Spanish, for example if password as empty, result.Errors[0] gave me "Passwords must be at least 6 characters.", but i need personalice the string to Spanish, this is an example but, i need change to spanish all errors messages.
Upvotes: 0
Views: 669
Reputation: 1054
I solved the problem
first tryed to validate ApplicationUser directly, but the text of errors show in english, so, i decided create a class with DataAnotations, then i validate this on the method, if the validatio as true, proceded to create a new user, if not valid, return false.
this is the Class:
public class CreatingUserModel
{
[Required(ErrorMessage ="El nombre para inicio de sesion es Requerido")]
[MinLength(6, ErrorMessage ="El Nombre para inicio de sesion debe contener al menos 6 caracteres")]
public string LoginName { get; set; }
[Required( ErrorMessage = "El Nombre del usuario es requerido" )]
public string Nombre { get; set; }
[Required( ErrorMessage = "El Apellido Paterno del usuario es requerido" )]
public string ApellidoPaterno { get; set; }
[Required( ErrorMessage = "El Apellido Materno del usuario es requerido" )]
public string ApellidoMaterno { get; set; }
[MaxLength( 10, ErrorMessage = "El largo maximo para la clave del trabajador es de 10 caracteres" )]
public string ClaveTrabajador { get; set; }
[Required(ErrorMessage ="El Email es requerido")]
[EmailAddress(ErrorMessage ="No es un email Valido")]
public string Email { get; set; }
[Required( ErrorMessage = "La contraseña es requerida" )]
[MinLength( 6, ErrorMessage = "La contraseña debe ser de al menos 6 caracteres" )]
public string Contraseña { get; set; }
[Required( ErrorMessage = "La confirmacion de la contraseña es requerida" )]
[Compare(nameof(Contraseña), ErrorMessage ="La contraseña confirmada no coincide")]
public string ConfirmeContraseña { get; set; }
public ApplicationUser ToApplicationUser()
{
return new ApplicationUser
{
Nombre = this.Nombre,
ApellidoPaterno = this.ApellidoPaterno,
ApellidoMaterno = this.ApellidoMaterno,
ClaveTrabajador = this.ClaveTrabajador,
UserName = this.LoginName,
Email = this.Email
};
}
}
and, to use:
private async Task< bool> addUser()
{
using (libProduccionDataBase.Contexto.DataBaseContexto context = new DataBaseContexto())
{
using (var t = new libProduccionDataBase.Identity.ApplicationUserManager( new libProduccionDataBase.Identity.ApplicationUserStore( context ) ))
{
CreatingUserModel _usr= new CreatingUserModel
{
Nombre = AddNombreKTbox.Text,
ApellidoPaterno = AddApPaternoKTbox.Text,
ApellidoMaterno = AddApMaternoKTbox.Text,
ClaveTrabajador = AddClaveKTbox.Text,
LoginName= AddLoginNameKTbox.Text,
Email = AddEmailKTbox.Text
};
ValidationContext validator = new ValidationContext(_usr);
List<ValidationResult> results = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(_usr, validator, results, true);
if (valid)
{
var usr= _usr.ToApplicationUser();
IdentityResult result = await t.CreateAsync(usr, AddPasswordKTbox.Text);
if (result.Succeeded) t.AddToRole( usr.Id, "Usuario General" );
return result.Succeeded;
}else
{
StringBuilder strbld = new StringBuilder();
results.ForEach( err => {
strbld.AppendFormat( "•{0}\n", err.ErrorMessage );
} );
Console.WriteLine( strbld.ToString() );
}
return false;
}
}
}
Upvotes: 1