Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change message "Passwords must have at least one non letter or digit character."

I am using Asp.Net Identity PasswordValidator and was hoping with some assistance with changing this default message so that it is more clear.

How can I change this message and include special character for example?

 manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 10,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true
        };
like image 650
Arianule Avatar asked Sep 18 '15 13:09

Arianule


1 Answers

Create a folder for IdentityExtensions, Add a class CustomPasswordValidator In that class you need to referance IIdentityValidator below is how i overide password Validation.

Here is a link

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace YourNameSpace.IdentityExtensions
{
    public class CustomPasswordValidator : IIdentityValidator<string>
    {
        public int RequiredLength { get; set; }
        public CustomPasswordValidator(int length)
        {
            RequiredLength = length;
        }

        public Task<IdentityResult> ValidateAsync(string item)
        {
            if (String.IsNullOrEmpty(item) || item.Length < RequiredLength)
            {
                return Task.FromResult(IdentityResult.Failed(String.Format("Password should be of length {0}", RequiredLength)));
            }

            string pattern = @"^(?=.*[0-9])(?=.*[!@#$%^&*])[0-9a-zA-Z!@#$%^&*0-9]{10,}$";

            if (!Regex.IsMatch(item, pattern))
            {
                return Task.FromResult(IdentityResult.Failed("Password should have one numeral and one special character"));
            }

            return Task.FromResult(IdentityResult.Success);
        }
    }
}
like image 137
pool pro Avatar answered Oct 27 '22 16:10

pool pro