Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Http;
  7. using Newtonsoft.Json;
  8.  
  9. namespace ConsoleClient
  10. {
  11. class Program
  12. {
  13. private const string APP_PATH = "http://51.15.100.12:5000";
  14. private static string token;
  15.  
  16. static void Main(string[] args)
  17. {
  18. Console.WriteLine("Введите логин:");
  19. string userName = Console.ReadLine();
  20.  
  21. Console.WriteLine("Введите пароль:");
  22. string password = Console.ReadLine();
  23.  
  24. var registerResult = Register(userName, password);
  25.  
  26. Console.WriteLine("Статусный код регистрации: {0}", registerResult);
  27.  
  28. Dictionary<string, string> tokenDictionary = GetTokenDictionary(userName, password);
  29. token = tokenDictionary["access_token"];
  30.  
  31. Console.WriteLine();
  32. Console.WriteLine("Access Token:");
  33. Console.WriteLine(token);
  34.  
  35. Console.WriteLine();
  36. string userInfo = GetUserInfo(token);
  37. Console.WriteLine("Пользователь:");
  38. Console.WriteLine(userInfo);
  39.  
  40. Console.WriteLine();
  41. string values = GetValues(token);
  42. Console.WriteLine("Values:");
  43. Console.WriteLine(values);
  44.  
  45. Console.Read();
  46. }
  47.  
  48. // регистрация
  49. static string Register(string email, string password)
  50. {
  51. var registerModel = new
  52. {
  53. Email = email,
  54. Password = password,
  55. ConfirmPassword = password
  56. };
  57. using (var client = new HttpClient())
  58. {
  59. var response = client.PostAsJsonAsync(APP_PATH + "/api/Account/Register", registerModel).Result;
  60. return response.StatusCode.ToString();
  61. }
  62. }
  63. // получение токена
  64. static Dictionary<string, string> GetTokenDictionary(string userName, string password)
  65. {
  66. var pairs = new List<KeyValuePair<string, string>>
  67. {
  68. new KeyValuePair<string, string>( "grant_type", "password" ),
  69. new KeyValuePair<string, string>( "username", userName ),
  70. new KeyValuePair<string, string> ( "Password", password )
  71. };
  72. var content = new FormUrlEncodedContent(pairs);
  73.  
  74. using (var client = new HttpClient())
  75. {
  76. var response =
  77. client.PostAsync(APP_PATH + "/Token", content).Result;
  78. var result = response.Content.ReadAsStringAsync().Result;
  79. // Десериализация полученного JSON-объекта
  80. Dictionary<string, string> tokenDictionary =
  81. JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
  82. return tokenDictionary;
  83. }
  84. }
  85.  
  86. // создаем http-клиента с токеном
  87. static HttpClient CreateClient(string accessToken = "")
  88. {
  89. var client = new HttpClient();
  90. if (!string.IsNullOrWhiteSpace(accessToken))
  91. {
  92. client.DefaultRequestHeaders.Authorization =
  93. new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
  94. }
  95. return client;
  96. }
  97.  
  98. // получаем информацию о клиенте
  99. static string GetUserInfo(string token)
  100. {
  101. using (var client = CreateClient(token))
  102. {
  103. var response = client.GetAsync(APP_PATH + "/api/Account/UserInfo").Result;
  104. return response.Content.ReadAsStringAsync().Result;
  105. }
  106. }
  107.  
  108. // обращаемся по маршруту api/values
  109. static string GetValues(string token)
  110. {
  111. using (var client = CreateClient(token))
  112. {
  113. var response = client.GetAsync(APP_PATH + "/api/values").Result;
  114. return response.Content.ReadAsStringAsync().Result;
  115. }
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement