41 lines
1.6 KiB
C#
41 lines
1.6 KiB
C#
using Microsoft.AspNet.Identity;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Threading.Tasks;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace EnVisage.Code.Validation
|
|
{
|
|
public class PrevuPasswordValidator : IIdentityValidator<string>
|
|
{
|
|
public int MinimumLength { get; set; }
|
|
public int MaximumLength { get; set; }
|
|
public PrevuPasswordValidator(int minLength, int maxLength)
|
|
{
|
|
MinimumLength = minLength;
|
|
MaximumLength = maxLength;
|
|
}
|
|
|
|
public Task<IdentityResult> ValidateAsync(string item)
|
|
{
|
|
if (String.IsNullOrEmpty(item) || item.Length < MinimumLength)
|
|
{
|
|
return Task.FromResult(IdentityResult.Failed(String.Format("Password should be at least of length {0}", MinimumLength)));
|
|
}
|
|
if (!String.IsNullOrEmpty(item) && item.Length > MaximumLength)
|
|
{
|
|
return Task.FromResult(IdentityResult.Failed(String.Format("Password should be maxiimum of length {0}", MaximumLength)));
|
|
}
|
|
string pattern = @"^(?=.{" + (MinimumLength - 1) + "," + (MaximumLength - 1) + "})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]).*$";
|
|
if (!Regex.IsMatch(item, pattern))
|
|
{
|
|
return Task.FromResult(IdentityResult.Failed("Password should have at least one lower letter, one upper letter, one numeral and one special character, but it should not start with special character"));
|
|
}
|
|
return Task.FromResult(IdentityResult.Success);
|
|
}
|
|
}
|
|
}
|
|
|