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

        }

        public override string ResetPassword(string username, string answer)

        {

            throw new NotImplementedException();

        }

        public override bool UnlockUser(string userName)

        {

            throw new NotImplementedException();

        }

        public override void UpdateUser(MembershipUser user)

        {

            throw new NotImplementedException();

        }

        public override bool ValidateUser(string username, string password)

        {

            MySqlConnection conn = null;

            try

            {

                //ОБЪЯВЛЯЕМ

                conn = new MySqlConnection(connectionString);

                conn.Open();

                string sql;

                MySqlDataAdapter adapter = null;

                DataSet data = new DataSet();

                //ДОСТАЕМ ПОЛЬЗОВАТЕЛЯ ПО ИМЕНИ

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

                adapter = new MySqlDataAdapter(sql, conn);

                adapter.Fill(data);

                if (data.Tables[0].Rows.Count > 0)

                {

                    //получаем строку из БД, преобразуем её в массив байтов, дешифруем, полученное приводим в строку, получаем пароль

                    //ЕСЛИ ПАРОЛИ СОВПАДАЮТ - ПРОПУСКАЕМ

                    if (Encoding.Default.GetString(DecryptPassword(Encoding.Default.GetBytes(data.Tables[0].Rows[0]["password"].ToString()))).Equals(password))

                    {

                        conn.Close();

                        return true;

                    }

                    else

                    {

                        conn.Close();

                        return false;

                    }

                }

                else

                {

                    conn.Close();

                    return false;

                }

            }

            catch (MySqlException exp)

            {

                conn.Close();

                return false;

            }

            catch (Exception exp)

            {

                conn.Close();

                return false;

            }

        }

    }

}

Е.2 Файл PowerPlayRolesProvider.cs

namespace powerplay

{

    public class PowerPlayRolesProvider: RoleProvider

    {

        string connectionString;

        string applicationName;

        public override void Initialize(string name, NameValueCollection config)

        {

            string database = "powerplay";

            string password_db = "admin";

            string user_db = "root";

            string server_db = "localhost";

            connectionString = ("server=" + server_db + "; user id=" + user_db + "; password=" + password_db + "; database=" + database + "");

            applicationName = "powerplay";

            base.Initialize(name, config);

        }

        //i-му юзеру назначается i-я роль.

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)

        {

            MySqlConnection conn = null;

            try

            {

                if (roleNames.Length < 1 || usernames.Length < 1) throw new ProviderException();

                else if (roleNames == null || roleNames[0] == "" || usernames == null || usernames[0] == "" || usernames.Length != roleNames.Length) throw new ProviderException();

                else

                {

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

                    conn = new MySqlConnection(connectionString);

                    conn.Open();

                    MySqlCommand comm;

                    MySqlDataReader reader = null;

                    string sql;

                    int[] id_role = null;

                    //ВЫТАСКИВАЕМ ID РОЛЕЙ

                    id_role = new int[roleNames.Length];