Exemple #1
0
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SqlConnection  sqlCon = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = c:\users\андрей\documents\visual studio 2017\Projects\Info Banks of Moscow\Info Banks of Moscow\Database1.mdf; Integrated Security = True");
                SqlDataAdapter sda    = new SqlDataAdapter("Select Count (*) From [Table] where Login='******'and Password='******'", sqlCon);


                SqlCommand sqlCommandPassword = new SqlCommand("Select Password From [Table] where Login='******'", sqlCon);
                sqlCon.Open();

                SqlDataReader sqlReaderPassword = sqlCommandPassword.ExecuteReader();

                string[] hash = new string[1];

                while (sqlReaderPassword.Read())
                {
                    string _hash = sqlReaderPassword.GetString(0);
                    hash[0] = _hash;
                }

                sqlReaderPassword.Close();
                sqlCon.Close();


                DataTable Table = new DataTable();
                sda.Fill(Table);

                if ((Table.Rows[0][0].ToString() == "1") && (CalculateHash(PasswordBox.Password) == CalculateHash(hash[0])))
                {
                    SqlCommand sqlCommand = new SqlCommand("Select Name From [Table] where Login='******'and Password='******'", sqlCon);
                    sqlCon.Open();

                    SqlDataReader sqlReader = sqlCommand.ExecuteReader();

                    List <User> Users = new List <User>();

                    while (sqlReader.Read())
                    {
                        User authorisedUser = new User(sqlReader.GetString(0), LoginTextBox.Text);
                        Users.Add(authorisedUser);
                    }

                    FileStream fs = new FileStream("name.txt", FileMode.Create);
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.Write(LoginTextBox.Text + " " + Users[0].Name);
                    }

                    ProfilePage ProfilePage = new ProfilePage();
                    NavigationService.Navigate(ProfilePage);
                }
                else
                {
                    MessageBox.Show("Пользователя с таким логином и паролем не существует", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch { MessageBox.Show("Что-то пошло не так...", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); }
        }
        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            ProfilePage ProfilePage = new ProfilePage();

            NavigationService.Navigate(ProfilePage);
        }