Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

VS 2019 Set ValidationAttribute value from appsettings.json

babyekc

New member
Joined
Jul 16, 2004
Messages
270
Hi all,

I have this:

Code:
[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:

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


and in appsettings.json, i have this:

Code:
  "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' ?
 

NeedSomeAnswers

Superbly Moderated
Joined
Jun 4, 2002
Messages
2,610
Ok so in asp.net 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)

Code:
 private void GetAppSettings(string contentRoot)
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(contentRoot)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables()
            .AddInMemoryCollection(
                new Dictionary<string, string> {
                    {"Timezone", "+1"}
                });
            Configuration = builder.Build();
        }

You want to call that method in Startup.cs by adding a new constructor method

Code:
public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            GetAppSettings(env.ContentRootPath);
        }

Once you have read the app setting in you can read them like this to get an individual value

Code:
string Settingval = Configuration.GetValue<string>("AppSettings:SettingName")

if you want to read all your app settings into a class than create a class eg

Code:
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

Code:
IConfigurationSection sec = Configuration.GetSection("PasswordPolicy");
services.Configure<PasswordPolicy>(sec);

Now in any controller you can inject your settings into the constructor

Code:
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.
 
Top