DNN Custom Authentication provider not displaying if default Auth provider is disabled

1.5k views Asked by At

I followed this guide as a template to creating my custom authentication provider DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

In that guide he creates his own authentication configuration class so that it does not share configuration with the default provider (which he built his custom provider off) which i converted to c#.

[Serializable()]
    public class ScanToLoginAuthConfig
    {
        #region "Private Members"

        private bool _Enabled = true;

        private int _portalId;
        private const string CACHEKEY = "Authentication.ScanToLogin.DNN";

        private const string ENABLED_SETTING_KEY = "DNN_ScanToLogin_Enabled";

        #endregion

        #region "Constructor(s)"

        protected ScanToLoginAuthConfig(int portalID)
        {
            _portalId = portalID;

            try
            {
                string setting = Null.NullString;
                if (PortalController.GetPortalSettingsDictionary(portalID)
                                    .TryGetValue(ENABLED_SETTING_KEY, out setting))
                {
                    _Enabled = bool.Parse(setting);
                }
                setting = Null.NullString;
            }
            catch
            {
            }
        }

        #endregion

        #region "Public Properties"

        public bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }

        [Browsable(false)]
        public int PortalId
        {
            get { return _portalId; }
            set { _portalId = value; }
        }

        #endregion

        #region "Public SHared Methods"

        public static void ClearConfig(int portalId)
        {
            string key = CACHEKEY + "_" + portalId.ToString();
            DataCache.RemoveCache(key);
        }

        public static ScanToLoginAuthConfig GetConfig(int portalId)
        {

            string key = CACHEKEY + "_" + portalId.ToString();
            ScanToLoginAuthConfig config = (ScanToLoginAuthConfig) DataCache.GetCache(key);

            if (config == null)
            {
                config = new ScanToLoginAuthConfig(portalId);
                DataCache.SetCache(key, config);
            }
            return config;
        }

        public static void UpdateConfig(ScanToLoginAuthConfig config)
        {
            PortalController.UpdatePortalSetting(config.PortalId, "DNN_Enabled", config.Enabled.ToString());
            ClearConfig(config.PortalId);
        }

        #endregion

    }

This works in so far as I con toggle my authentication provider as enabled or disabled independently of the default provider, however, my provider does not load if the default provider is not enabled.

if (PortalController.GetPortalSettingsDictionary(portalID)
                                .TryGetValue(ENABLED_SETTING_KEY, out setting))
            {
                _Enabled = bool.Parse(setting);
            }

When I go in to modify the settings the above code fires, but it never finds the value specified by "ENABLED_SETTING_KEY" ("DNN_ScanToLogin_Enabled"). The portal settings dictionary (for that portal ID) contains 14 items and "DNN_ScanToLogin_Enabled" is not in there. Can anyone tell me why that is, PLEASE!

EDIT I've modified things somewhat

public static void ClearConfig(int portalId)
        {
            string key = CACHEKEY + "_" + portalId.ToString();
            DataCache.RemoveCache(key);
        }

and

public static ScanToLoginAuthConfig GetConfig(int portalId)
        {

            string key = CACHEKEY + "_" + portalId.ToString();

Have become

public static void UpdateConfig(ScanToLoginAuthConfig config)
        {
            PortalController.UpdatePortalSetting(config.PortalId, "DNN_ScanToLogin_Enabled", config.Enabled.ToString());
            ClearConfig(config.PortalId);
        }

and

public static ScanToLoginAuthConfig GetConfig(int portalId)
        {
            string key = CACHEKEY;// +"_" + portalId.ToString();

Respectively. So now the keys seem to update and set correctly (for my unspecified definition of correctly...) However when I disable the default authentication provider and enable my authentication provider, it still does not display my provider and instead shows the default. When both default and mine are enabled, then both display, tabbed.

I also updated the following to include the key's name as it is defined at the top of the code.

public static void UpdateConfig(ScanToLoginAuthConfig config)
        {
            PortalController.UpdatePortalSetting(config.PortalId, "DNN_ScanToLogin_Enabled", config.Enabled.ToString());
            ClearConfig(config.PortalId);
        }
2

There are 2 answers

0
Fix It Scotty On

The first thing I would confirm is that when you installed your Authentication Provider under Host > Extensions, you configured the Enabled flag there.

BUT, to enable it for a particular Portal, you need to go to Admin > Extensions, open the Authentication Systems section, click Edit on your Provider, and Enable it there.

If you still have problems, you may issues with your settings implementation. (If you implemented settings using the AuthenticationSettingsBase).

0
Hamed Abbasi On

you must change the code inside your login file and use your ScanToLoginAuthConfig class for Enabled property

public override bool Enabled
    {
        get
        {
            return ScanToLoginAuthConfig.GetConfig(PortalId).Enabled;
        }
    }