Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义asp.net-identity错误字符串_C#_Asp.net Identity - Fatal编程技术网

C# 自定义asp.net-identity错误字符串

C# 自定义asp.net-identity错误字符串,c#,asp.net-identity,C#,Asp.net Identity,Im尝试对asp.net-identity的字符串错误进行个性化设置,这是我的代码: private async Task< bool> addUser() { using (libProduccionDataBase.Contexto.DataBaseContexto context = new DataBaseContexto()) { using (var t = new libProduccionDat

Im尝试对asp.net-identity的字符串错误进行个性化设置,这是我的代码:

    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;
            }
        }
    }
private async TaskaddUser()
{
使用(libProduccionDataBase.Contexto.DataBaseContexto context=newdatabasecontexto())
{
使用(var t=new libproducciondabase.Identity.ApplicationUserManager(new libproducciondabase.Identity.ApplicationUserStore(上下文)))
{
ApplicationUser usr=新的ApplicationUser
{
Nombre=AddNombreKTbox.Text,
apellidopterno=AddApPaternoKTbox.Text,
ApellidoMaterno=addapaternoktbox.Text,
ClaveTrabajador=AddClaveKTbox.Text,
UserName=addloginnametbox.Text,
Email=AddEmailKTbox.Text
};
IdentityResult result=await t t.CreateAsync(usr,AddPasswordKTbox.Text);
if(result.successed)t.AddToRole(usr.Id,“Usuario General”);
foreach(result.Errors中的变量err)
{
控制台写入线(错误);
}
返回结果。成功;
}
}
}

但我需要西班牙文的错误,例如,如果密码为空,则返回结果。错误[0]给了我“密码必须至少为6个字符”。但我需要将字符串个性化为西班牙文,这是一个示例,但我需要将所有错误消息更改为西班牙文。

我解决了问题。

首先尝试直接验证ApplicationUser,但错误文本以英语显示,因此,我决定使用DataAnotations创建一个类,然后我在方法上验证它,如果validatio为true,则创建一个新用户,如果无效,则返回false

这是一节课:

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
        };

    }
}
以及使用:

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;
            }
        }
    }
private async TaskaddUser()
{
使用(libProduccionDataBase.Contexto.DataBaseContexto context=newdatabasecontexto())
{
使用(var t=new libproducciondabase.Identity.ApplicationUserManager(new libproducciondabase.Identity.ApplicationUserStore(上下文)))
{
CreatingUserModel _usr=新CreatingUserModel
{
Nombre=AddNombreKTbox.Text,
apellidopterno=AddApPaternoKTbox.Text,
ApellidoMaterno=addapaternoktbox.Text,
ClaveTrabajador=AddClaveKTbox.Text,
LoginName=addloginname.Text,
Email=AddEmailKTbox.Text
};
ValidationContext validator=新的ValidationContext(_usr);
列表结果=新列表();
bool valid=Validator.TryValidateObject(_usr,Validator,results,true);
如果(有效)
{
var usr=_usr.ToApplicationUser();
IdentityResult result=await t t.CreateAsync(usr,AddPasswordKTbox.Text);
if(result.successed)t.AddToRole(usr.Id,“Usuario General”);
返回结果。成功;
}否则
{
StringBuilder strbld=新的StringBuilder();
results.ForEach(err=>{
strbld.AppendFormat(“•{0}\n”,err.ErrorMessage);
} );
Console.WriteLine(strbld.ToString());
}
返回false;
}
}
}

阅读本地化是你的出路。