Results 1 to 2 of 2

Thread: Set ValidationAttribute value from appsettings.json

  1. #1

    Thread Starter
    Hyperactive Member babyekc's Avatar
    Join Date
    Jul 2004
    planet earth

    Set ValidationAttribute value from appsettings.json

    Hi all,

    I have this:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
        public class PasswordPolicyAttribute : ValidationAttribute
            private readonly int _minimumCharacters;
            private readonly int _maximumCharacters;
            private readonly int _maximumConsecutiveCharacters;
            private readonly bool _requiredUppercase;
            private readonly bool _requiredLowercase;
            private readonly bool _requiredNumericCharacters;
            private readonly bool _requiredSpecialCharacters;
            public PasswordPolicyAttribute(int minimumCharacters = 0,
                                           int maximumCharacters = 0,
                                           int maximumConsecutiveCharacters = 0,
                                           bool requiredUppercase = false,
                                           bool requiredLowercase = false,
                                           bool requiredNumericCharacters = false,
                                           bool requiredSpecialCharacters = false)
                _minimumCharacters = minimumCharacters;
                _maximumCharacters = maximumCharacters;
                _maximumConsecutiveCharacters = maximumConsecutiveCharacters;
                _requiredUppercase = requiredUppercase;
                _requiredLowercase = requiredLowercase;
                _requiredNumericCharacters = requiredNumericCharacters;
                _requiredSpecialCharacters = requiredSpecialCharacters;
    and in the ViewModel, i have this:

    [PasswordPolicy(3, 10, 3, true, true, true, true)]
    public string password_new { get; set; }

    and in appsettings.json, i have this:

      "PasswordPolicy": {
        "MinimumCharacters": 2,
        "MaximumCharacters": 2,
        "MaximumConsecutiveCharacters": 2,
        "RequiredUppercase": true,
        "RequiredLowercase": true,
        "RequiredNumericCharacter": true,
        "RequiredSpecialCharacter": true,
    how can i read values from appsettings and set the '3, 10, 3, true, true, true, true' ?
    *wink wink*


  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Manchester uk

    Re: Set ValidationAttribute value from appsettings.json

    Ok so in core which as your using appsettings.json i assume your using, you read in the settings like this (add this method to your startup.cs)

     private void GetAppSettings(string contentRoot)
                var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    new Dictionary<string, string> {
                        {"Timezone", "+1"}
                Configuration = builder.Build();
    You want to call that method in Startup.cs by adding a new constructor method

    public Startup(IConfiguration configuration, IWebHostEnvironment env)
    Once you have read the app setting in you can read them like this to get an individual value

    string Settingval = Configuration.GetValue<string>("AppSettings:SettingName")
    if you want to read all your app settings into a class than create a class eg

    public class PasswordPolicy
       public string Settings1{ get; set; }
       public string Settings2{ get; set; }
    then get the whole section and inject it into your service layer

    IConfigurationSection sec = Configuration.GetSection("PasswordPolicy");
    Now in any controller you can inject your settings into the constructor

    public class LoginController : Controller
      public LoginController(IOptions<PasswordPolicy> pp)
    You can't then apply those settings to the view model as that is static, what you should probably do is do your checks in the controller post method when the login form is submitted.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width