Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. using App8.Services;
  2. using App8.ViewModels;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Xamarin.Essentials;
  11. using Xamarin.Forms;
  12. using Xamarin.Forms.Xaml;
  13.  
  14. namespace App8
  15. {
  16. [XamlCompilation(XamlCompilationOptions.Compile)]
  17. public partial class Login : ContentPage
  18. {
  19. public Login()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24.  
  25. public async Task<bool> LoginAsync(string login, string senha)
  26. {
  27. var KeyValues = new List<KeyValuePair<string, string>>
  28. {
  29. new KeyValuePair<string, string >("login", login),
  30. new KeyValuePair<string, string >("senha", senha),
  31.  
  32. };
  33.  
  34.  
  35. var request = new HttpRequestMessage(
  36. HttpMethod.Post, "http://10.83.1.72:3000/auth");
  37.  
  38. request.Content = new FormUrlEncodedContent(KeyValues);
  39.  
  40. var client = new HttpClient();
  41. var response = await client.SendAsync(request);
  42. var token = await response.Content.ReadAsStringAsync();
  43.  
  44.  
  45.  
  46.  
  47. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  48.  
  49. {
  50.  
  51. await SaveToken("token", token);
  52. await SaveToken("login", login);
  53. await SaveToken("senha", senha);
  54.  
  55. return true;
  56.  
  57.  
  58. }
  59. else
  60. {
  61. return false;
  62.  
  63.  
  64. }
  65.  
  66.  
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74. public async Task<bool> SaveToken(string chave, string valor)
  75. {
  76. try
  77. {
  78. if (string.IsNullOrWhiteSpace(valor))
  79. {
  80. await SecureStorage.SetAsync(chave, valor);
  81. return true;
  82.  
  83. }
  84.  
  85. return false;
  86. }
  87. catch
  88. {
  89. return false;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. public async Task VerificarLogin()
  96. {
  97. LoginViewModel usuario = new LoginViewModel()
  98. {
  99. login = Convert.ToString(login.Text),
  100. senha = Convert.ToString(senha.Text)
  101. };
  102. if (await LoginAsync(usuario.login, usuario.senha) == true)
  103. {
  104. var token = await SecureStorage.GetAsync("token");
  105. var login = await SecureStorage.GetAsync("login");
  106. var senha = await SecureStorage.GetAsync("senha");
  107. await DisplayAlert("Alert", login + "Usuario com Sucesso!", "OK");
  108. await Navigation.PushAsync(new PageTwo()); /// COLOQUEI AQUI A PAGINA 2
  109. }
  110. else
  111. {
  112.  
  113. await DisplayAlert("Alert", "Usuario ou Senha incorretos", "OK");
  114. }
  115.  
  116. }
  117.  
  118. public async Task<bool> verificarUsuarioLogado()
  119. {
  120.  
  121. if (await SecureStorage.GetAsync("token") != null)
  122. {
  123. return true;
  124.  
  125. }
  126. else
  127. {
  128. return false;
  129. }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. private async void Button_Clicked(object sender, EventArgs e)
  138. {
  139. if (await verificarUsuarioLogado())
  140. {
  141. await Navigation.PushAsync(new PageTwo());
  142.  
  143. }
  144. else
  145. {
  146. await VerificarLogin();
  147.  
  148. }
  149.  
  150. }
  151.  
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement