Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using System;
  2. using System.Net.Http;
  3. using Newtonsoft.Json;
  4. using System.Text;
  5. using System.Collections.Generic;
  6.  
  7. using Xamarin.Forms;
  8.  
  9. namespace Hackatown
  10. {
  11. public partial class Login : ContentPage
  12. {
  13. public Login()
  14. {
  15. InitializeComponent();
  16. NavigationPage.SetHasNavigationBar(this, false);
  17. usernameEntry.Completed += (s, e) => passwordEntry.Focus();
  18. passwordEntry.Completed += BtnLogin_Clicked;
  19. btnLogin.Clicked += BtnLogin_Clicked;
  20. }
  21.  
  22. async void BtnLogin_Clicked(object sender, EventArgs e)
  23. {
  24. btnLogin.IsEnabled = false;
  25. try
  26. {
  27. var client = new HttpClient();
  28. client.BaseAddress = new Uri("https://lassondehacks.io:8080/");
  29.  
  30. string usernameLabel = "\"" + "username" + "\"";
  31. string passwordLabel = "\"" + "password" + "\"";
  32. string username = "\"" + usernameEntry.Text + "\"";
  33. string password = "\"" + passwordEntry.Text + "\"";
  34.  
  35. string jsonData = @"{" + usernameLabel + " : " + username + ", " + passwordLabel + " : " + password + "}";
  36.  
  37. var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
  38. HttpResponseMessage response = await client.PostAsync("/auth/login", content);
  39. var result = await response.Content.ReadAsStringAsync();
  40. AuthentificationDecrypter auth = JsonConvert.DeserializeObject<AuthentificationDecrypter>(result);
  41. if (auth.success)
  42. {
  43. await Navigation.PushModalAsync(new MasterPage(auth));
  44. btnLogin.IsEnabled = true;
  45. usernameEntry.Text = "";
  46. passwordEntry.Text = "";
  47. }
  48. else {
  49. IsBusy = false;
  50. XFToast.LongMessage("Erreur de connexion, nom d'utilisateur ou mot de passe incorrect");
  51. btnLogin.IsEnabled = true;
  52. passwordEntry.Text = "";
  53. }
  54. }
  55. catch(Exception exep) {
  56. IsBusy = false;
  57. XFToast.LongMessage("Erreur de connexion");
  58. btnLogin.IsEnabled = true;
  59.  
  60. }
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement