private void AddProvider()
        {
            var adConfig = CustomConfiguration.Settings.ActiveDirectoryData();
            var provider = new ActiveDirectoryProvider();
            var config   = new NameValueCollection
            {
                ["name"] = CalmoProviderName,
                ["connectionStringName"] = adConfig.ConnectionStringName,
                ["connectionUsername"]   = adConfig.UserName,
                ["connectionPassword"]   = adConfig.Password,
                ["attributeMapUsername"] = "******"
            };

            provider.Initialize(CalmoProviderName, config);

            var membershipProviders = new MembershipProviderCollection {
                provider
            };

            membershipProviders.SetReadOnly();

            var bindingFlags   = BindingFlags.NonPublic | BindingFlags.Static;
            var membershipType = typeof(Membership);

            membershipType.GetField("s_Initialized", bindingFlags)?.SetValue(null, true);
            membershipType.GetField("s_InitializeException", bindingFlags)?.SetValue(null, null);
            membershipType.GetField("s_Provider", bindingFlags)?.SetValue(null, provider);
            membershipType.GetField("s_Providers", bindingFlags)?.SetValue(null, membershipProviders);
        }
Beispiel #2
0
        public Provider()
        {
            try
            {
                SqlMembershipProvider sqlMembershipProvider = new SqlMembershipProvider();
                NameValueCollection   config = new NameValueCollection();
                config.Add("connectionStringName", "myConnection");
                config.Add("enablePasswordRetrieval", "false");
                config.Add("enablePasswordReset", "true");
                config.Add("requiresQuestionAndAnswer", "false");
                config.Add("applicationName", "MyApp");
                config.Add("requiresUniqueEmail", "true");
                config.Add("maxInvalidPasswordAttempts", "3");
                config.Add("passwordAttemptWindow", "5");
                config.Add("commandTimeout", "30");
                config.Add("name", "AspNetSqlMembershipProvider");
                config.Add("minRequiredPasswordLength", "9");
                config.Add("minRequiredNonalphanumericCharacters", "1");
                sqlMembershipProvider.Initialize(config["name"], config);

                MembershipProviderCollection membershipProviders = new MembershipProviderCollection();
                membershipProviders.Add(sqlMembershipProvider);
                membershipProviders.SetReadOnly();

                BindingFlags bindingFlags   = BindingFlags.NonPublic | BindingFlags.Static;
                Type         membershipType = typeof(Membership);
                membershipType.GetField("s_Initialized", bindingFlags).SetValue(null, true);
                membershipType.GetField("s_InitializeException", bindingFlags).SetValue(null, null);
                membershipType.GetField("s_HashAlgorithmType", bindingFlags).SetValue(null, "SHA-512");
                membershipType.GetField("s_HashAlgorithmFromConfig", bindingFlags).SetValue(null, false);
                membershipType.GetField("s_UserIsOnlineTimeWindow", bindingFlags).SetValue(null, 15);
                membershipType.GetField("s_Provider", bindingFlags).SetValue(null, sqlMembershipProvider);
                membershipType.GetField("s_Providers", bindingFlags).SetValue(null, membershipProviders);

                var connectionString = Membership.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                if (connectionString != null)
                {
                    connectionString.SetValue(Membership.Provider, DBCommands._Connection);
                }

                NewPasswordNeeded();
            }
            catch (Exception ex)
            {
                DBCommands.RecordError(ex);
            }
        }
        public void InjectMembershipImplementation(IEnumerable <KeyValuePair <string, string> > simulatedAppConfigSettings)
        {
            if (MembershipIsInitialized)
            {
                _originalProvider = MembershipProvider;
            }
            else
            {
                _originalProvider = null;
            }

            _injectedProvider = GetProvider();

            InitializeMembershipProviderFromConfigEntry(_injectedProvider, simulatedAppConfigSettings);

            PostInitializeUpdate(_injectedProvider);

            MembershipProvider      = _injectedProvider;
            MembershipIsInitialized = true;

            MembershipProviders = new MembershipProviderCollection();
            MembershipProviders.Add(_injectedProvider);
            MembershipProviders.SetReadOnly();
        }