Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tor Langlo 191 posts 554 karma points
    Feb 01, 2023 @ 17:32
    Tor Langlo
    0

    How do I get a hold of an instance of IPasswordConfiguration?

    I Have a need for accessing the minimum password length in code, and I see that this value can be found by accessing an instance of IPasswordConfiguration. The ultimate source is normally the security settings in appSettings.json.

    At this point the only way to do this that I've found is to inject an IMemberManager instance, typecast it to MemberManager, and access the MemberManager.PasswordConfiguration property. That's obviously a bit of a hack, is there a better way?

    public MyController(IMemberManager memberManager)
    {
        if (memberManager is MemberManager memberManagerImpl)
        {
            var pwdConfig = memberManagerImpl.PasswordConfiguration;
            int minLength = pwdConfig.RequiredLength;
        }
    }
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 01, 2023 @ 19:54
    Huw Reddick
    0

    Can't you just inject IPasswordConfiguration ?

    public myController( IPasswordConfiguration passwordConfig)
    {
        int minLength = passwordConfig.RequiredLength;
    }
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Feb 01, 2023 @ 20:03
    Huw Reddick
    0

    Ignore that, you can't just inject IPasswordConfiguration

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 02, 2023 @ 07:59
    Dave Woestenborghs
    101

    Hi Tor,

    You can find the required settings here. https://docs.umbraco.com/umbraco-cms/reference/configuration/securitysettings

    To use them in code you will need to do the following :

        public MyController(IOptions<SecuritySettings> securitySettings)
        {
           int minMemberPwLength =  this.securitySettings.Value.MemberPassword.RequiredLength;
    int minUserPwLength =  this.securitySettings.Value.UserPassword.RequiredLength;
        }
    

    Dave

  • Tor Langlo 191 posts 554 karma points
    Feb 02, 2023 @ 17:52
    Tor Langlo
    0

    Thanks Dave, that's exactly what I was looking for. I may have ignored that approach because I was afraid the IOptions<> wouldn't contain the default settings, but they do indeed contain defaults, e.g. RequiredLength = 10 if you don't specify it in appSettings.json.

    A small hint to the documentation team to include a paragraph or two on how to access the settings in code.

Please Sign in or register to post replies

Write your reply to:

Draft