コード例 #1
0
        /// <summary>
        /// System.Configuration.Provider.ProviderBase.Initialize Method.
        /// </summary>
        public override void Initialize(string name, NameValueCollection config)
        {
            // Initialize values from web.config.
            if (config == null)
                throw new ArgumentNullException("config", Properties.Resources.ErrArgumentNull);

            if (string.IsNullOrEmpty(name))
                name = Properties.Resources.MembershipProviderDefaultName;

            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", Properties.Resources.MembershipProviderDefaultDescription);
            }

            // Initialize the abstract base class.
            base.Initialize(name, config);

            m_applicationName = GetConfigValue(config["applicationName"], HostingEnvironment.ApplicationVirtualPath);
            m_maxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"), CultureInfo.InvariantCulture);
            m_passwordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"], "10"), CultureInfo.InvariantCulture);
            m_minRequiredNonAlphanumericCharacters = Convert.ToInt32(GetConfigValue(config["minRequiredNonAlphanumericCharacters"], "1"), CultureInfo.InvariantCulture);
            m_minRequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minRequiredPasswordLength"], "7"), CultureInfo.InvariantCulture);
            m_passwordStrengthRegularExpression = GetConfigValue(config["passwordStrengthRegularExpression"], "");
            m_enablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "true"), CultureInfo.InvariantCulture);
            m_enablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["enablePasswordRetrieval"], "true"), CultureInfo.InvariantCulture);
            m_requiresQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"], "false"), CultureInfo.InvariantCulture);
            m_requiresUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"], "true"), CultureInfo.InvariantCulture);

            // Get password encryption type.
            string pwFormat = GetConfigValue(config["passwordFormat"], "Hashed");
            switch (pwFormat)
            {
                case "Hashed":
                    m_passwordFormat = MembershipPasswordFormat.Hashed;
                    break;
                case "Encrypted":
                    m_passwordFormat = MembershipPasswordFormat.Encrypted;
                    break;
                case "Clear":
                    m_passwordFormat = MembershipPasswordFormat.Clear;
                    break;
                default:
                    throw new ProviderException(Properties.Resources.ErrPwFormatNotSupported);
            }

            // Get connection string.
            m_connectionString = GetConnectionString(config["connectionStringName"]);

            // Check whether we are on a web hosted application or not
            // If we're web hosted use the Web.config; otherwise the application's config file
            Configuration cfg = HttpContext.Current != null
                                    ? WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath)
                                    : ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            // Get encryption and decryption key information from the configuration.
            m_machineKeyConfig = (MachineKeySection)cfg.GetSection("system.web/machineKey");

            if (!m_passwordFormat.Equals(MembershipPasswordFormat.Clear))
            {
                if (m_machineKeyConfig == null)
                    throw new ProviderException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ErrConfigSectionNotFound, "system.web/machineKey"));

                if (m_machineKeyConfig.ValidationKey.Contains("AutoGenerate"))
                    throw new ProviderException(Properties.Resources.ErrAutoGeneratedKeyNotSupported);
            }
        }