示例#1
0
        private static bool InitializeSettings(bool initializeGeneralSettings, RuntimeConfig appConfig, MembershipSection settings)
        {
            if (!initializeGeneralSettings)
            {
                return(false);
            }

            s_HashAlgorithmType       = settings.HashAlgorithmType;
            s_HashAlgorithmFromConfig = !string.IsNullOrEmpty(s_HashAlgorithmType);
            if (!s_HashAlgorithmFromConfig)
            {
                // If no hash algorithm is specified, use the same as the "validation" in "<machineKey>".
                // If the validation is "3DES", switch it to use "SHA1" instead.
                MachineKeyValidation v = appConfig.MachineKey.Validation;
                if (v != MachineKeyValidation.AES && v != MachineKeyValidation.TripleDES)
                {
                    s_HashAlgorithmType = appConfig.MachineKey.ValidationAlgorithm;
                }
                else
                {
                    s_HashAlgorithmType = "SHA1";
                }
            }
            s_Providers = new MembershipProviderCollection();
            if (HostingEnvironment.IsHosted)
            {
                ProvidersHelper.InstantiateProviders(settings.Providers, s_Providers, typeof(MembershipProvider));
            }
            else
            {
                foreach (ProviderSettings ps in settings.Providers)
                {
                    Type t = Type.GetType(ps.Type, true, true);
                    if (!typeof(MembershipProvider).IsAssignableFrom(t))
                    {
                        throw new ArgumentException(SR.GetString(SR.Provider_must_implement_type, typeof(MembershipProvider).ToString()));
                    }
                    MembershipProvider  provider    = (MembershipProvider)Activator.CreateInstance(t);
                    NameValueCollection pars        = ps.Parameters;
                    NameValueCollection cloneParams = new NameValueCollection(pars.Count, StringComparer.Ordinal);
                    foreach (string key in pars)
                    {
                        cloneParams[key] = pars[key];
                    }
                    provider.Initialize(ps.Name, cloneParams);
                    s_Providers.Add(provider);
                }
            }

            TimeSpan timeWindow = settings.UserIsOnlineTimeWindow;

            s_UserIsOnlineTimeWindow = (int)timeWindow.TotalMinutes;

            return(true);
        }
示例#2
0
 private static bool InitializeSettings(bool initializeGeneralSettings, RuntimeConfig appConfig, MembershipSection settings)
 {
     if (!initializeGeneralSettings)
     {
         return(false);
     }
     s_HashAlgorithmType       = settings.HashAlgorithmType;
     s_HashAlgorithmFromConfig = !string.IsNullOrEmpty(s_HashAlgorithmType);
     if (!s_HashAlgorithmFromConfig)
     {
         MachineKeyValidation validation = appConfig.MachineKey.Validation;
         if ((validation != MachineKeyValidation.AES) && (validation != MachineKeyValidation.TripleDES))
         {
             s_HashAlgorithmType = appConfig.MachineKey.ValidationAlgorithm;
         }
         else
         {
             s_HashAlgorithmType = "SHA1";
         }
     }
     s_Providers = new MembershipProviderCollection();
     if (HostingEnvironment.IsHosted)
     {
         ProvidersHelper.InstantiateProviders(settings.Providers, s_Providers, typeof(MembershipProvider));
     }
     else
     {
         foreach (ProviderSettings settings2 in settings.Providers)
         {
             Type c = Type.GetType(settings2.Type, true, true);
             if (!typeof(MembershipProvider).IsAssignableFrom(c))
             {
                 throw new ArgumentException(System.Web.SR.GetString("Provider_must_implement_type", new object[] { typeof(MembershipProvider).ToString() }));
             }
             MembershipProvider  provider   = (MembershipProvider)Activator.CreateInstance(c);
             NameValueCollection parameters = settings2.Parameters;
             NameValueCollection config     = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
             foreach (string str in parameters)
             {
                 config[str] = parameters[str];
             }
             provider.Initialize(settings2.Name, config);
             s_Providers.Add(provider);
         }
     }
     s_UserIsOnlineTimeWindow = (int)settings.UserIsOnlineTimeWindow.TotalMinutes;
     return(true);
 }
        private void GetPasswordHelper(bool requireQA, bool enablePasswordRetrieval, string answer)
        {
            MembershipCreateStatus status;
            provider = new MembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("connectionStringName", _connStrName);
            config.Add("requiresQuestionAndAnswer", requireQA ? "true" : "false");
            config.Add("enablePasswordRetrieval", enablePasswordRetrieval ? "true" : "false");
            config.Add("passwordFormat", "clear");
            config.Add("applicationName", _applicationName);
            config.Add("writeExceptionsToEventLog", "false");
            provider.Initialize(null, config);

            provider.CreateUser("foo", "barbar!", "*****@*****.**", "color", "blue", true, null, out status);

            try
            {
                string password = provider.GetPassword("foo", answer);
                if (!enablePasswordRetrieval)
                    Assert.Fail("This should have thrown an exception");
                Assert.AreEqual("barbar!", password);
            }
            catch (MembershipPasswordException)
            {
                if (requireQA && answer != null)
                    Assert.Fail("This should not have thrown an exception");
            }
            catch (ProviderException)
            {
                if (requireQA && answer != null)
                    Assert.Fail("This should not have thrown an exception");
            }
        }
        public void InitializeMembershipProviderFromConfigEntry(MembershipProvider result,
            IEnumerable<KeyValuePair<string, string>> simulatedAppConfigSettings)
        {
            NameValueCollection nameValueCollection = null;

            MembershipSection membership = ConfigurationManager.GetSection("system.web/membership") as MembershipSection;

            foreach (ProviderSettings settings in membership.Providers)
            {
                if (settings.Name == FixtureConstants.NameOfConfiguredMembershipProvider)
                {
                    nameValueCollection = new NameValueCollection(settings.Parameters);
                    break;
                }
            }

            if (nameValueCollection == null)
            {
                throw new Exception("Configuration not found for membership provider RavenDBMembership.");
            }

            nameValueCollection["connectionStringName"] = "StubConnectionString";

            foreach (var kvp in simulatedAppConfigSettings)
            {
                ValidateConfigurationValue(kvp.Key, kvp.Value);
                nameValueCollection.Set(kvp.Key, kvp.Value);
            }

            result.Initialize(FixtureConstants.NameOfConfiguredMembershipProvider, nameValueCollection);
        }