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
    Location
    planet earth
    Posts
    270

    Set ValidationAttribute value from appsettings.json

    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' ?
    *wink wink*

    _babyekc

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,612

    Re: Set ValidationAttribute value from appsettings.json

    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.
    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