public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
      if (config == null)
      {
        NullArgumentException("config");
      }
      if (string.IsNullOrEmpty(name))
      {
        name = "MySqlExtendedMembershipProvider";
      }
      if (string.IsNullOrEmpty(config["description"]))
      {
        config.Remove("description");
        config.Add("description", string.Format("MySql Default {0} Description", name));
      }

      base.Initialize(name, config);

      var appName = GetConfigValue(config["applicationName"], HostingEnvironment.SiteName);
      _maxPwdAttempts = Int32.Parse(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
      _pwdAttemptWindow = Int32.Parse(GetConfigValue(config["passwordAttemptWindow"], "10"));
      _minReqNonAlphanumericalChars = Int32.Parse(GetConfigValue(config["minRequiredNonalphanumericCharacters"], "1"));
      _minReqPwdLength = Int32.Parse(GetConfigValue(config["minRequiredPasswordLength"], "7"));
      _pwdStrenghtRegex = GetConfigValue(config["passwordStrengthRegularExpression"], "");
      _enablePwdReset = bool.Parse(GetConfigValue(config["enablePasswordReset"], "True"));
      _enablePwdRetrival = bool.Parse(GetConfigValue(config["enablePasswordRetrieval"], "False"));
      _reqQuestionAnswer = bool.Parse(GetConfigValue(config["requiresQuestionAndAnswer"], "False"));
      _reqUniqueEmail = bool.Parse(GetConfigValue(config["requiresUniqueEmail"], "True"));

      var pwdFormat = !string.IsNullOrEmpty(config["passwordFormat"]) ? config["passwordFormat"].ToString().ToLowerInvariant() : "hashed";

      switch (pwdFormat)
      {
        case "hashed":
          _pwdFormat = MembershipPasswordFormat.Hashed;
          break;
        case "encrypted":
          _pwdFormat = MembershipPasswordFormat.Encrypted;
          break;
        case "clear":
          _pwdFormat = MembershipPasswordFormat.Clear;
          break;
        default:
          throw new ProviderException(Resources.PasswordFormatNotSupported);
      }

      if (_pwdFormat == MembershipPasswordFormat.Hashed)
      {
        if (_enablePwdRetrival)
          throw new ProviderException(Resources.CannotRetrieveHashedPasswords);
      }

      _app = new Application(appName, base.Description);
      ConnectionStringSettings connStrSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
      _connString = connStrSettings != null ? connStrSettings.ConnectionString.Trim() : "";
      if (string.IsNullOrEmpty(_connString)) return;

      UserTableName = GetConfigValue(config["userTableName"], "");
      UserIdColumn = GetConfigValue(config["userIdColumn"], "");
      UserNameColumn = GetConfigValue(config["userNameColumn"], "");
      _autoGenerateTables = bool.Parse(GetConfigValue(config["autoGenerateTables"], "True"));
      if (_autoGenerateTables)
        CreateTables();
      else
        ValidateUserTable();

      Initialized = true;
    }
Esempio n. 2
0
    /// <summary>
    /// Initializes the provider.
    /// </summary>
    /// <param name="name">The friendly name of the provider.</param>
    /// <param name="config">A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.</param>
    /// <exception cref="T:System.ArgumentNullException">The name of the provider is null.</exception>
    /// <exception cref="T:System.ArgumentException">The name of the provider has a length of zero.</exception>
    /// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"/> on a provider after the provider has already been initialized.</exception>
    public override void Initialize(string name, NameValueCollection config)
    {
      if (config == null)
        throw new ArgumentNullException("config");

      if (name == null || name.Length == 0)
        name = "MySQLProfileProvider";

      if (string.IsNullOrEmpty(config["description"]))
      {
        config.Remove("description");
        config.Add("description", "MySQL Profile provider");
      }
      base.Initialize(name, config);

      try
      {
        string applicationName = GetConfigValue(config["applicationName"], HostingEnvironment.ApplicationVirtualPath);

        connectionString = "";
        ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings[
            config["connectionStringName"]];
        if (ConnectionStringSettings != null)
          connectionString = ConnectionStringSettings.ConnectionString.Trim();

        if (String.IsNullOrEmpty(connectionString)) return;

        // make sure our schema is up to date
        SchemaManager.CheckSchema(connectionString, config);

        app = new Application(applicationName, base.Description);
      }
      catch (Exception ex)
      {
        throw new ProviderException(Resources.ErrorInitProfileProvider, ex);
      }
    }
Esempio n. 3
0
        /// <summary>
        /// Initializes the provider with the property values specified in the ASP.NET application configuration file
        /// </summary>
        /// <param name="name">The name of the provider instance to initialize.</param>
        /// <param name="config">Object that contains the names and values of configuration options for the provider.
        /// </param>
        public override void Initialize(string name, NameValueCollection config)
        {
            //Initialize values from web.config.
              if (config == null)
            throw new ArgumentException("config");
              if (name == null || name.Length == 0)
            throw new ArgumentException("name");
              if (String.IsNullOrEmpty(config["description"]))
              {
            config.Remove("description");
            config["description"] = "MySQL Session State Store Provider";
              }
              base.Initialize(name, config);
              string applicationName = HostingEnvironment.ApplicationVirtualPath;
              if (!String.IsNullOrEmpty(config["applicationName"]))
            applicationName = config["applicationName"];

              // Get <sessionState> configuration element.
              Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
              sessionStateConfig = (SessionStateSection)webConfig.SectionGroups["system.web"].Sections["sessionState"];

              // Initialize connection.
              connectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
              if (connectionStringSettings == null || connectionStringSettings.ConnectionString.Trim() == "")
            throw new HttpException("Connection string can not be blank");
              connectionString = connectionStringSettings.ConnectionString;

              writeExceptionsToEventLog = false;
              if (config["writeExceptionsToEventLog"] != null)
              {
            writeExceptionsToEventLog = (config["writeExceptionsToEventLog"].ToUpper() == "TRUE");
              }

              // Make sure we have the correct schema.
              SchemaManager.CheckSchema(connectionString, config);
              app = new Application(applicationName, base.Description);

              // Get the application id.
              try
              {
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
              conn.Open();
              app.EnsureId(conn);
              CheckStorageEngine(conn);
              cleanupInterval = GetCleanupInterval(conn);
            }
              }
              catch (MySqlException e)
              {
            HandleMySqlException(e, "Initialize");
              }

              // Setup the cleanup timer
              if (cleanupInterval <= 0)
            cleanupInterval = 1;
              cleanupTimer = new Timer(new TimerCallback(CleanupOldSessions), null, 0,
              cleanupInterval * 1000 * 60);
        }
        /// <summary>
        /// Initializes the MySQL membership provider with the property values specified in the 
        /// ASP.NET application's configuration file. This method is not intended to be used directly 
        /// from your code. 
        /// </summary>
        /// <param name="name">The name of the <see cref="MySQLMembershipProvider"/> instance to initialize.</param>
        /// <param name="config">A collection of the name/value pairs representing the 
        /// provider-specific attributes specified in the configuration for this provider.</param>
        /// <exception cref="T:System.ArgumentNullException">config is a null reference.</exception>
        /// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"/> on a provider after the provider has already been initialized.</exception>
        /// <exception cref="T:System.Configuration.Provider.ProviderException"></exception>
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
              {
            throw new ArgumentNullException("config");
              }
              if (name == null || name.Length == 0)
              {
            name = "MySQLMembershipProvider";
              }
              if (string.IsNullOrEmpty(config["description"]))
              {
            config.Remove("description");
            config.Add("description", "MySQL default application");
              }
              base.Initialize(name, config);

              string applicationName = GetConfigValue(config["applicationName"],
              HostingEnvironment.ApplicationVirtualPath);
              maxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
              passwordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"], "10"));
              minRequiredNonAlphanumericCharacters =
              Convert.ToInt32(GetConfigValue(config["minRequiredNonalphanumericCharacters"], "1"));
              minRequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minRequiredPasswordLength"], "7"));
              passwordStrengthRegularExpression =
              Convert.ToString(GetConfigValue(config["passwordStrengthRegularExpression"], ""));
              enablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "True"));
              enablePasswordRetrieval = Convert.ToBoolean(
              GetConfigValue(config["enablePasswordRetrieval"], "False"));
              requiresQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"], "False"));
              requiresUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"], "True"));
              writeExceptionsToEventLog = Convert.ToBoolean(GetConfigValue(config["writeExceptionsToEventLog"], "True"));
              string temp_format = config["passwordFormat"];

              if (temp_format == null)
            temp_format = "hashed";
              else
            temp_format = temp_format.ToLowerInvariant();

              if (temp_format == "hashed")
            passwordFormat = MembershipPasswordFormat.Hashed;
              else if (temp_format == "encrypted")
            passwordFormat = MembershipPasswordFormat.Encrypted;
              else if (temp_format == "clear")
            passwordFormat = MembershipPasswordFormat.Clear;
              else
            throw new ProviderException("Password format not supported.");

              // if the user is asking for the ability to retrieve hashed passwords, then let
              // them know we can't
              if (PasswordFormat == MembershipPasswordFormat.Hashed)
              {
            if (EnablePasswordRetrieval)
              throw new ProviderException(Resources.CannotRetrieveHashedPasswords);
              }

              ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings[
              config["connectionStringName"]];
              if (ConnectionStringSettings != null)
            connectionString = ConnectionStringSettings.ConnectionString.Trim();
              else
            connectionString = "";

              if (String.IsNullOrEmpty(connectionString)) return;

              // make sure we have the correct schema
              SchemaManager.CheckSchema(connectionString, config);

              app = new Application(applicationName, base.Description);
        }
    /// <summary>
    /// Initializes the provider with the property values specified in the ASP.NET application configuration file
    /// </summary>
    /// <param name="name">The name of the provider instance to initialize.</param>
    /// <param name="config">Object that contains the names and values of configuration options for the provider.
    /// </param>
    public override void Initialize(string name, NameValueCollection config)
    {
      //Initialize values from web.config.
      if (config == null)
        throw new ArgumentException("config");
      if (name == null || name.Length == 0)
        throw new ArgumentException("name");
      if (String.IsNullOrEmpty(config["description"]))
      {
        config.Remove("description");
        config["description"] = "MySQL Session State Store Provider";
      }
      base.Initialize(name, config);
      string applicationName = HostingEnvironment.ApplicationVirtualPath;
      if (!String.IsNullOrEmpty(config["applicationName"]))
        applicationName = config["applicationName"];

      // Get <sessionState> configuration element.
      Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
      sessionStateConfig = (SessionStateSection)webConfig.SectionGroups["system.web"].Sections["sessionState"];

      // Initialize connection.
      connectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
      if (connectionStringSettings == null || connectionStringSettings.ConnectionString.Trim() == "")
        throw new HttpException("Connection string can not be blank");
      connectionString = connectionStringSettings.ConnectionString;

      writeExceptionsToEventLog = false;
      if (config["writeExceptionsToEventLog"] != null)
      {
        writeExceptionsToEventLog = (config["writeExceptionsToEventLog"].ToUpper() == "TRUE");
      }

      enableExpireCallback = false;

      if (config["enableExpireCallback"] != null)
      {
        enableExpireCallback = (config["enableExpireCallback"].ToUpper() == "TRUE");
      }

      // Make sure we have the correct schema.
      SchemaManager.CheckSchema(connectionString, config);
      app = new Application(applicationName, base.Description);

      // Get the application id.
      try
      {
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
          conn.Open();
          app.EnsureId(conn);
          CheckStorageEngine(conn);         
        }
      }
      catch (MySqlException e)
      {
        HandleMySqlException(e, "Initialize");
      }

      // Add  cleanup interval
      MySqlTransaction mySqlTransaction = null;
      try
      {
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
          MySqlCommand cmd = new MySqlCommand(
              "INSERT IGNORE INTO my_aspnet_sessioncleanup SET" +
              " ApplicationId = @ApplicationId, " +
              " LastRun = NOW(), " +
              " IntervalMinutes = 10",
             conn);
          cmd.Parameters.AddWithValue("@ApplicationId", ApplicationId);
          conn.Open();
          mySqlTransaction = conn.BeginTransaction();
          cmd.ExecuteNonQuery();
          mySqlTransaction.Commit();
          cleanupInterval = GetCleanupInterval(conn, ApplicationId);
        }
      }
      catch (MySqlException e)
      {
        if (mySqlTransaction != null)
        {
          try
          {
            Trace.WriteLine("Initialize: Attempt to rollback");
            mySqlTransaction.Rollback();
          }
          catch (MySqlException ex)
          {
            HandleMySqlException(ex, "Initialize: Rollback Failed");
          }
        }
        HandleMySqlException(e, "Initialize");
      }
      finally
      {
        if (mySqlTransaction != null)
          mySqlTransaction.Dispose();
      }

   
      // Setup the cleanup timer
      if (cleanupInterval <= 0)
        cleanupInterval = 1;
      cleanupTimer = new Timer(new TimerCallback(CleanupOldSessions), null, 0,
          cleanupInterval * 1000 * 60);
    }
    /// <summary>
    /// Initializes settings values for Personalization Provider
    /// </summary>
    /// <param name="name"></param>
    /// <param name="config"></param>
    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
      if (config == null)
        throw new ArgumentNullException("config");

      if (string.IsNullOrEmpty(name))
      {
        name = "MySqlPersonalizationProvider";
      }

      if (string.IsNullOrEmpty(config["description"]))
      {
        config.Remove("description");
        config.Add("description", "MySql Personalization provider");      
      }

      base.Initialize(name, config);

      string applicationName = HostingEnvironment.ApplicationVirtualPath;
      if (!String.IsNullOrEmpty(config["applicationName"]))
        applicationName = config["applicationName"];


      if (!(config["writeExceptionsToEventLog"] == null))
      {
        if (config["writeExceptionsToEventLog"].ToUpper() == "TRUE")
        {
          writeExceptionsToEventLog = true;
        }
      }

      connectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
      if (connectionStringSettings != null)
        connectionString = connectionStringSettings.ConnectionString.Trim();
      else
        connectionString = "";

      if (String.IsNullOrEmpty(connectionString)) return;

      // Make sure we have the correct schema.
      SchemaManager.CheckSchema(connectionString, config);

      app = new Application(applicationName, base.Description);

      // Get the application id.
      try
      {
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
          conn.Open();
          app.EnsureId(conn);          
        }
      }
      catch (Exception ex)
      {
        if (writeExceptionsToEventLog)
          WriteToEventLog(ex, "MySQLPersonalizationProvider - Initialize");        
        throw;
      }  
    }
        /// <summary>
        /// Initializes the provider.
        /// </summary>
        /// <param name="name">The friendly name of the provider.</param>
        /// <param name="config">A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider.</param>
        /// <exception cref="T:System.ArgumentNullException">The name of the provider is null.</exception>
        /// <exception cref="T:System.ArgumentException">The name of the provider has a length of zero.</exception>
        /// <exception cref="T:System.InvalidOperationException">An attempt is made to call <see cref="M:System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection)"/> on a provider after the provider has already been initialized.</exception>
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
              {
            throw new ArgumentNullException("config");
              }
              if (name == null || name.Length == 0)
              {
            name = "MySQLRoleProvider";
              }
              if (string.IsNullOrEmpty(config["description"]))
              {
            config.Remove("description");
            config.Add("description", "MySQL Role provider");
              }
              base.Initialize(name, config);

              string applicationName = HostingEnvironment.ApplicationVirtualPath;
              if (!String.IsNullOrEmpty(config["applicationName"]))
            applicationName = config["applicationName"];

              if (!(config["writeExceptionsToEventLog"] == null))
              {
            if (config["writeExceptionsToEventLog"].ToUpper() == "TRUE")
            {
              pWriteExceptionsToEventLog = true;
            }
              }
              pConnectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
              if (pConnectionStringSettings != null)
            connectionString = pConnectionStringSettings.ConnectionString.Trim();
              else
            connectionString = "";

              if (String.IsNullOrEmpty(connectionString)) return;

              // make sure our schema is up to date
              SchemaManager.CheckSchema(connectionString, config);

              app = new Application(applicationName, Description);
        }