Guest User

Untitled

a guest
Jan 8th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Vaersacces
  11. {
  12. //Plantilla para datos del Login
  13. public class LoginData
  14. {
  15. string user;
  16. string pass;
  17.  
  18. public LoginData(string user, string pass)
  19. {
  20. this.user = user;
  21. this.pass = pass;
  22. }
  23.  
  24. }
  25.  
  26. //Clase Asíncrona para llamar al Web Service
  27. public class AsyncTaskLogin
  28. {
  29.  
  30. //Instancia de un nuevo cliente
  31. static HttpClient client = new HttpClient();
  32.  
  33. //Se asignan los datos a la nueva instancia
  34. static async Task RunAsync()
  35. {
  36. client.BaseAddress = new Uri("dirección de la api");
  37. client.DefaultRequestHeaders.Accept.Clear();
  38. client.DefaultRequestHeaders.Accept.Add(
  39. new MediaTypeWithQualityHeaderValue("application/json"));
  40. }
  41.  
  42. //Método para postear un objeto tipo LoginData en formato Json
  43. static async Task<Uri> CreateLoginAsync(LoginData login)
  44. {
  45. HttpResponseMessage response = await client.PostAsJsonAsync(
  46. "login/users", login);
  47. response.EnsureSuccessStatusCode();
  48.  
  49. return response.Headers.Location;
  50. }
  51.  
  52. }
  53. }
  54.  
  55. using System;
  56. using System.Collections.Generic;
  57. using System.Linq;
  58. using System.Web;
  59. using System.Web.UI;
  60. using System.Web.UI.WebControls;
  61.  
  62. namespace Vaersacces
  63. {
  64. public partial class WebForm1 : System.Web.UI.Page
  65. {
  66. protected void Page_Load(object sender, EventArgs e)
  67. {
  68.  
  69. }
  70.  
  71. //Evento de Login
  72. protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
  73. {
  74.  
  75. //Se comprueba si el usuario es correcto
  76. bool authenticated = false;
  77. authenticated = LoginCorrecto(Login1.UserName, Login1.Password);
  78. e.Authenticated = authenticated;
  79.  
  80. if (authenticated)
  81. {
  82. Response.Redirect("Default.aspx");
  83. }
  84.  
  85. //Método para comprobar la veracidad del usuario
  86. bool LoginCorrecto(string user, string pass)
  87. {
  88. LoginData data = new LoginData(user, pass);
  89.  
  90. AsyncTaskLogin login = new AsyncTaskLogin();
  91.  
  92. var uri = await login.CreateLoginAsync(data);
  93.  
  94. return false;
  95. }
  96.  
  97. }
  98. }
  99. }
Add Comment
Please, Sign In to add comment