private void Form1_Load(object sender, EventArgs e) { List <User> Users; using (AllegroAPIClientAccountsEntities AllegroEntities = new AllegroAPIClientAccountsEntities()) { Users = AllegroEntities.Users.ToList(); } if (Users.Count() != 0) { foreach (var user in Users) { int countryCode = 1; long hashoffset = 0; long serverTime = 0; //deklaracje zmiennych zwracanych przez funkcję doLogin string sessionHandle; using (SHA256 sha256 = new SHA256Managed()) { string pass = Cryptography.Decryptdata(user.password); byte[] passwordHash = sha256.ComputeHash(Encoding.ASCII.GetBytes(pass)); string encodedPassword = Convert.ToBase64String(passwordHash); try //logowanie może się przecież nie powieść - wtedy zostanie zwrócony wyjątek, który musimy złapać { sessionHandle = client.doLoginEnc( user.login, //login został wpisany w polu tekstowym encodedPassword, //hasło countryCode, //parametr "countrycode", lista wartości w dokumentacji webApiKey, //klucz webapi - zadeklarowany wcześniej jako stała localVersion, //klucz wersji - jw. out hashoffset, // out serverTime); // dodatkowe wartości zwracane przez funkcję. } catch (Exception E) { listBox1.Items.Add("Logowanie " + user.login + " nie powiodło się"); //jeżeli logowanie nie powiodło się - wyświetlamy komunikat listBox1.Items.Add(E.ToString()); return; } } UserWithSession userWithSession = new UserWithSession(user); userWithSession.sessionHandle = sessionHandle; UsersWithSession.Add(userWithSession); LoggedUsers.Items.Add(userWithSession.login); } DisplaySellItems(); } }
private void button1_Click(object sender, EventArgs e) { if ((!string.IsNullOrWhiteSpace(textBoxLogin.Text) && !string.IsNullOrWhiteSpace(textBoxPassword.Text)) == true) { string login = textBoxLogin.Text; string password = textBoxPassword.Text; int countryCode = 1; long hashoffset = 0; long serverTime = 0; //deklaracje zmiennych zwracanych przez funkcję doLogin using (SHA256 sha256 = new SHA256Managed()) { byte[] passwordHash = sha256.ComputeHash(Encoding.ASCII.GetBytes(password)); string encodedPassword = Convert.ToBase64String(passwordHash); try //logowanie może się przecież nie powieść - wtedy zostanie zwrócony wyjątek, który musimy złapać { string sessionHandle = client.doLoginEnc( login, //login został wpisany w polu tekstowym encodedPassword, //hasło countryCode, //parametr "countrycode", lista wartości w dokumentacji webApiKey, //klucz webapi - zadeklarowany wcześniej jako stała localVersion, //klucz wersji - jw. out hashoffset, // out serverTime); // dodatkowe wartości zwracane przez funkcję. using (AllegroAPIClientAccountsEntities AllegroEntities = new AllegroAPIClientAccountsEntities()) { User user = new User() { login = login, password = Cryptography.Encryptdata(password), }; if (AllegroEntities.Users.Any(a => a.login == user.login)) { MessageBox.Show("Taki użytkownik już istnieje."); } else { AllegroEntities.Users.Add(user); try { AllegroEntities.SaveChanges(); UserWithSession userWithSession = new UserWithSession(user); userWithSession.sessionHandle = sessionHandle; UsersWithSession.Add(userWithSession); MessageBox.Show("Dodano pomyślnie."); LoggedUsers.Items.Add(userWithSession.login); } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { Debug.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage); } } } } } } catch (Exception E) { listBox1.Items.Add("Logowanie nie powiodło się"); //jeżeli logowanie nie powiodło się - wyświetlamy komunikat listBox1.Items.Add(E.ToString()); return; } } } else { MessageBox.Show("Nie podano loginu lub hasła"); } }