Создание веб-приложения, сочетающего в себе проектирование и создание баз данных, работу со сторонними форматами данных, программирование в NET и веб-разработку, страница 58

            minRequiredNonAlphanumericCharacters = 0;

            minRequiredPasswordLength = 6;

            passwordStrengthRegularExpression = String.Empty;

            enablePasswordReset = true;

            enablePasswordRetrieval = true;

            requiresQuestionAndAnswer = false;

            requiresUniqueEmail = true;

            newPasswordLength = 6;

            passwordFormat = MembershipPasswordFormat.Encrypted;

            base.Initialize(name, config);

        }

        public override string ApplicationName

        {

            get

            {

                return applicationName;

            }

            set

            {

                applicationName = value;

            }

        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)

        {

            throw new NotImplementedException();

        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)

        {

            throw new NotImplementedException();

        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

        {

            MySqlConnection conn = null;

            try

            {

                //ОБЪЯВЛЕНИЕ

                conn = new MySqlConnection(connectionString);

                conn.Open();

                string sql;

                MySqlCommand comm = null;

                MySqlDataAdapter adapter = null;

                DataSet data = new DataSet();

                //ПРОВЕРЯЕМ, ЕСТЬ ЛИ УЖЕ ТАКОЙ ЮЗЕР

                sql = "SELECT * FROM auth_user WHERE name = '" + username + "' or email = '" + email + "';";

                comm = new MySqlCommand(sql, conn);

                adapter = new MySqlDataAdapter(sql, conn);

                adapter.Fill(data);

                if (data.Tables[0].Rows.Count != 0)

                {

                    status = MembershipCreateStatus.ProviderError;

                    conn.Close();

                    for (int i = 0; i<data.Tables[0].Rows.Count; i++)

                    {

                        if (data.Tables[0].Rows[i]["name"].ToString().Equals(username)) status = MembershipCreateStatus.DuplicateUserName;

                        else if (data.Tables[0].Rows[i]["email"].ToString().Equals(email)) status = MembershipCreateStatus.DuplicateEmail;

                    }

                    return null;

                }

                //ЕСЛИ НЕТ - СОЗДАЕМ ЮЗЕРА

                else

                {

                    sql = "INSERT INTO auth_user (id, name, password, email, role) VALUES (default, '"+username+"', '"+ Encoding.Default.GetString(EncryptPassword(Encoding.Default.GetBytes(password))) +"', '"+email+"', 1)";

                    comm = new MySqlCommand(sql, conn);

                    comm.ExecuteNonQuery();

                    conn.Close();

                    status = MembershipCreateStatus.Success;

                    MembershipUser user = new MembershipUser(providerName, username, providerUserKey, email, passwordQuestion, "", isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);

                    return user;

                }             

            }

            catch (MySqlException exp)

            {

                conn.Close();

                status = MembershipCreateStatus.ProviderError;

                return null;

            }

        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)

        {

            throw new NotImplementedException();

        }

        public override bool EnablePasswordReset