2015-09-18 106 views
3

我正在使用Asp.Net Identity PasswordValidator,並希望在更改此默認消息方面提供一些幫助,以便更清楚。更改消息「密碼必須至少包含一個非字母或數字字符。」

如何更改此消息幷包含特殊字符?

manager.PasswordValidator = new PasswordValidator 
     { 
      RequiredLength = 10, 
      RequireNonLetterOrDigit = true, 
      RequireDigit = true, 
      RequireLowercase = true, 
      RequireUppercase = true 
     }; 

回答

5

IdentityExtensions創建一個文件夾,添加一個類CustomPasswordValidator在該類需要全球化志願服務青年IIdentityValidator下面是我在此改變密碼驗證。

這裏是一個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])(?=.*[[email protected]#$%^&*])[[email protected]#$%^&*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); 
     } 
    } 
} 
+0

酷,感謝的人! – Arianule

+0

沒問題,很高興我可以幫忙 –

相關問題