26

How can i customize Asp.net Identity 2 username already taken validation message(Name XYZ is already taken.)? Thanks

Nazmul Hossain
  • 2,085
  • 5
  • 25
  • 34
  • 1
    I think you need to take a look at the identity model and figure out if there is any special attribute for that. Or look at the account controller – qamar Dec 26 '14 at 10:04
  • 4
    @qamar. In this particular case you are wrong because all the messages are embedded into Identity resources – Alex Art. Dec 26 '14 at 12:17

5 Answers5

21

Well, I didn't find any simple solution to this issue. And by simple i mean modifying some message in a attribute/model/controller.

One possible solution could be:

After executing

var result = await UserManager.CreateAsync(user, model.Password);

In case that result is not successful you can check it's Errors property for the "Name XYZ is already taken." pattern and replace it with your custom message.

Another solution (this is my preferred way) is to write a custom UserValidation class:

 /// <summary>
    ///     Validates users before they are saved to an IUserStore
    /// </summary>
    /// <typeparam name="TUser"></typeparam>
    public class CustomUserValidator<TUser> : UserValidator<TUser, string>
        where TUser : ApplicationUser
    {
        /// <summary>
        ///     Constructor
        /// </summary>
        /// <param name="manager"></param>
        public CustomUserValidator(UserManager<TUser, string> manager) : base(manager)
        {
            this.Manager = manager;
        }

        private UserManager<TUser, string> Manager { get; set; }

        /// <summary>
        ///     Validates a user before saving
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public override async Task<IdentityResult> ValidateAsync(TUser item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            var errors = new List<string>();
            await ValidateUserName(item, errors);
            if (RequireUniqueEmail)
            {
                await ValidateEmail(item, errors);
            }
            if (errors.Count > 0)
            {
                return IdentityResult.Failed(errors.ToArray());
            }
            return IdentityResult.Success;
        }

        private async Task ValidateUserName(TUser user, List<string> errors)
        {
            if (string.IsNullOrWhiteSpace(user.UserName))
            {
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "Name"));
            }
            else if (AllowOnlyAlphanumericUserNames && !Regex.IsMatch(user.UserName, @"^[A-Za-z0-9@_\.]+$"))
            {
                // If any characters are not letters or digits, its an illegal user name
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidUserName, user.UserName));
            }
            else
            {
                var owner = await Manager.FindByNameAsync(user.UserName);
                if (owner != null && !EqualityComparer<string>.Default.Equals(owner.Id, user.Id))
                {
                    errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateName, user.UserName));
                }
            }
        }

        // make sure email is not empty, valid, and unique
        private async Task ValidateEmail(TUser user, List<string> errors)
        {
            if (!user.Email.IsNullOrWhiteSpace())
            {
                if (string.IsNullOrWhiteSpace(user.Email))
                {
                    errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "Email"));
                return;
                }
                try
                {
                    var m = new MailAddress(user.Email);
                }
                catch (FormatException)
                {
                    errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidEmail, email));
                return;
                }
            }
            var owner = await Manager.FindByEmailAsync(user.Email);
            if (owner != null && !EqualityComparer<string>.Default.Equals(owner.Id, user.Id))
            {
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateEmail, email));
            }
        }
    }

You can see that for all the validation error messages Resources being used, So by specifying a custom format in your resources you will be able to customize those messages.

You can register your validator in ApplicationUserManager class, Create method:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
{
   manager.UserValidator = new CustomUserValidator<ApplicationUser>(manager)
   {
       AllowOnlyAlphanumericUserNames = false,
       RequireUniqueEmail = true
   };
}
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
  • 33
    It is unbelievable that you are required to implement your own validator in order to change a string. – Justin Skiles Aug 23 '15 at 14:59
  • And I don't even see how you can replace the error message without creating a new `IdentityResult` since `Errors` is a read-only IEnumerable. – mellis481 Apr 18 '16 at 19:16
  • When trying this I get the error `'Resources' is inaccessible due to its protection level`. If I check the class Microsoft.AspNet.Identity.Resources it is marked as internal. – Ogglas Aug 24 '16 at 12:57
  • Solved `'Resources' is inaccessible due to its protection level` here: https://stackoverflow.com/questions/39123039/asp-net-identity-2-0-extend-uservalidator-with-custom-unique-property/39125762#39125762 – Ogglas Aug 24 '16 at 14:15
  • Your answer( stackoverflow.com/questions/39123039/…) is basically the same. You probably missed those lines: `Another solution (this is my preferred way) is to write a custom UserValidation class... You can see that for all the validation error messages Resources being used, So by specifying a custom format in your resources you will be able to customize those messages.` – Alex Art. Aug 24 '16 at 14:26
19

It can be done much easier than the accepted answer.

Add a class and inherit it from IdentityErrorDescriber

public class AppErrorDescriber : IdentityErrorDescriber
    {
        public override IdentityError DuplicateUserName(string userName)
        {
            var error = base.DuplicateUserName(userName);
            error.Description = "This email address has already been registered. Please log in.";
            return error;
        }
    }

Now use the new class in your Startup.cs and that's it.

services.AddDefaultIdentity<AppUser>(options => ... )
                .AddErrorDescriber<AppErrorDescriber>();
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Ihor Deyneka
  • 1,326
  • 1
  • 19
  • 37
2

Just customize your AddErrors method like this:

private void AddErrors(IdentityResult result)
{
    foreach (var error in result.Errors)
    {
        if (error.StartsWith("Name"))
        {
            var NameToEmail= Regex.Replace(error,"Name","Email");
            ModelState.AddModelError("", NameToEmail);
        }
        else
        {
            ModelState.AddModelError("", error);
        }
    }
}
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
0

Use XLocalizer - Nuget package for localization. See docs here.

Hrvoje
  • 13,566
  • 7
  • 90
  • 104
-1

The easy way to add your own property to ApplicationUser class like:

 public class AppUser:IdentityUser
  {
     public string MyUserName{get; set;}
  }
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Manfice
  • 149
  • 7