0

I have created a registration form in User Identity Project . I want to translate this message but I cant find the location of source message . The error message is shown in picture. Can anyone help me ? enter image description here

Here is the Related View Model for password: enter image description here

Chipset
  • 1
  • 2

2 Answers2

0

You should override methods of IdentityErrorDescriber to change identity error messages.

public class CustomIdentityErrorDescriber : IdentityErrorDescriber
{
    public override IdentityError PasswordTooShort(int length) { return new IdentityError { Code = nameof(PasswordTooShort), Description = $"Passwords must be at least {length} characters."  }; }
    public override IdentityError PasswordRequiresNonAlphanumeric() { return new IdentityError { Code = nameof(PasswordRequiresNonAlphanumeric), Description = "Passwords must have at least one non alphanumeric character." }; }
    public override IdentityError PasswordRequiresDigit() { return new IdentityError { Code = nameof(PasswordRequiresDigit), Description = "Passwords must have at least one digit ('0'-'9')." }; }
    public override IdentityError PasswordRequiresLower() { return new IdentityError { Code = nameof(PasswordRequiresLower), Description = "Passwords must have at least one lowercase ('a'-'z')." }; }
    public override IdentityError PasswordRequiresUpper() { return new IdentityError { Code = nameof(PasswordRequiresUpper), Description = "Passwords must have at least one uppercase ('A'-'Z')." }; }
}

and then you need replace default IdentityErrorDescriber in startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // ...   
    services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddErrorDescriber<YourIdentityErrorDescriber>();
}

The answer is from https://stackoverflow.com/a/38199890/5426333

evilGenius
  • 1,041
  • 1
  • 7
  • 16
0

In Asp Net Framework ( The traditional one , not the Core ) there is not a simple way to change or customize Registration Error Messages. Hence , one simple way is changing or translating error messages one by one. First trying to see which error message appear in the screen upon user wrong data , then writing a code in AddError method for changing them. In this way, you have to edit the built-in AddError method. Here is my solution :

private void AddErrors(IdentityResult result)
    {
        foreach (var error in result.Errors)
        {
            
            if (error.StartsWith("Passwords") && error.Contains("non letter"))
            {
                ModelState.AddModelError("", "Write your own message");
            }
            
            
            else if (error.StartsWith("Passwords") && error.Contains("one digit"))
            {
                ModelState.AddModelError("", "write your own message  ");
            }

             else if("Put More Conditions Here ")
                  { "Put More Declarations Here"}

            else
            {
                ModelState.AddModelError("", error);
            }                

    }

I tried this way and it works smoothly !

Chipset
  • 1
  • 2