Search code examples
c#asp.net-core.net-coreasp.net-identity

.NET core validation attributes based on configuration values


I am looking for a way to inject values from the configuration (options) into the parameters of validation attributes.

The scenario where this hit me was the scaffolded identity UI.

It gives the possibility to configure options about the password length. But changes aren't respected on the generated register page. This is because on the page the values of the validation attributes there are hardcoded.

Anyone know if this is even possible?


Solution

  • If i'm not wrong, you are trying to do some kind a thing below which is not possible :

    public int passLength = 3;
    public class Person
    {
      [MaxLength(passLength)]
      public DateTime? DateOfBirth { get; set; }
    }
    

    There is no workaround for this as far as i know. You can try custom validator and use Configuration service as you wish. You can check this sample code

    public class CustomPasswordAttribute : ValidationAttribute
    {
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
        var configuration = (IConfiguration)validationContext
                .GetService(typeof(IConfiguration));
    
        if (!(value is String)) 
        {
          return new ValidationResult("Should be string");
        }
    
        int.TryParse(configuration["Validation:PasswordLength"], out int passLength);
    
        if (value.ToString().Length != passLength)
        {
          return new ValidationResult("Wrong Length");
        }
    
        return ValidationResult.Success;
      }
    }
    
    public class UserModel
    {
      [CustomPassword]
      public string Password { get; set; }
    }