临河任务调度
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.6KB

  1. using System;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. using IdentityModel.Client;
  5. using Microsoft.Extensions.Configuration;
  6. using Himp.TaskScheduling.Samples;
  7. using Volo.Abp.DependencyInjection;
  8. using Volo.Abp.IdentityModel;
  9. namespace Himp.TaskScheduling;
  10. public class ClientDemoService : ITransientDependency
  11. {
  12. private readonly ISampleAppService _sampleAppService;
  13. private readonly IIdentityModelAuthenticationService _authenticationService;
  14. private readonly IConfiguration _configuration;
  15. public ClientDemoService(
  16. ISampleAppService sampleAppService,
  17. IIdentityModelAuthenticationService authenticationService,
  18. IConfiguration configuration)
  19. {
  20. _sampleAppService = sampleAppService;
  21. _authenticationService = authenticationService;
  22. _configuration = configuration;
  23. }
  24. public async Task RunAsync()
  25. {
  26. await TestWithDynamicProxiesAsync();
  27. await TestWithHttpClientAndIdentityModelAuthenticationServiceAsync();
  28. await TestAllManuallyAsync();
  29. }
  30. /* Shows how to perform an HTTP request to the API using ABP's dynamic c# proxy
  31. * feature. It is just simple as calling a local service method.
  32. * Authorization and HTTP request details are handled by the ABP framework.
  33. */
  34. private async Task TestWithDynamicProxiesAsync()
  35. {
  36. Console.WriteLine();
  37. Console.WriteLine($"***** {nameof(TestWithDynamicProxiesAsync)} *****");
  38. var result = await _sampleAppService.GetAsync();
  39. Console.WriteLine("Result: " + result.Value);
  40. result = await _sampleAppService.GetAuthorizedAsync();
  41. Console.WriteLine("Result (authorized): " + result.Value);
  42. }
  43. /* Shows how to use HttpClient to perform a request to the HTTP API.
  44. * It uses ABP's IIdentityModelAuthenticationService to simplify obtaining access tokens.
  45. */
  46. private async Task TestWithHttpClientAndIdentityModelAuthenticationServiceAsync()
  47. {
  48. Console.WriteLine();
  49. Console.WriteLine($"***** {nameof(TestWithHttpClientAndIdentityModelAuthenticationServiceAsync)} *****");
  50. //Get access token using ABP's IIdentityModelAuthenticationService
  51. var accessToken = await _authenticationService.GetAccessTokenAsync(
  52. new IdentityClientConfiguration(
  53. _configuration["IdentityClients:Default:Authority"],
  54. _configuration["IdentityClients:Default:Scope"],
  55. _configuration["IdentityClients:Default:ClientId"],
  56. _configuration["IdentityClients:Default:ClientSecret"],
  57. _configuration["IdentityClients:Default:GrantType"],
  58. _configuration["IdentityClients:Default:UserName"],
  59. _configuration["IdentityClients:Default:UserPassword"]
  60. )
  61. );
  62. //Perform the actual HTTP request
  63. using (var httpClient = new HttpClient())
  64. {
  65. httpClient.SetBearerToken(accessToken);
  66. var url = _configuration["RemoteServices:TaskScheduling:BaseUrl"] +
  67. "api/TaskScheduling/sample/authorized";
  68. var responseMessage = await httpClient.GetAsync(url);
  69. if (responseMessage.IsSuccessStatusCode)
  70. {
  71. var responseString = await responseMessage.Content.ReadAsStringAsync();
  72. Console.WriteLine("Result: " + responseString);
  73. }
  74. else
  75. {
  76. throw new Exception("Remote server returns error code: " + responseMessage.StatusCode);
  77. }
  78. }
  79. }
  80. /* Shows how to use HttpClient to perform a request to the HTTP API.
  81. */
  82. private async Task TestAllManuallyAsync()
  83. {
  84. Console.WriteLine();
  85. Console.WriteLine($"***** {nameof(TestAllManuallyAsync)} *****");
  86. //Obtain access token from the IDS4 server
  87. // discover endpoints from metadata
  88. var client = new HttpClient();
  89. var disco = await client.GetDiscoveryDocumentAsync(_configuration["IdentityClients:Default:Authority"]);
  90. if (disco.IsError)
  91. {
  92. Console.WriteLine(disco.Error);
  93. return;
  94. }
  95. // request token
  96. var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
  97. {
  98. Address = disco.TokenEndpoint,
  99. ClientId = _configuration["IdentityClients:Default:ClientId"],
  100. ClientSecret = _configuration["IdentityClients:Default:ClientSecret"],
  101. UserName = _configuration["IdentityClients:Default:UserName"],
  102. Password = _configuration["IdentityClients:Default:UserPassword"],
  103. Scope = _configuration["IdentityClients:Default:Scope"]
  104. });
  105. if (tokenResponse.IsError)
  106. {
  107. Console.WriteLine(tokenResponse.Error);
  108. return;
  109. }
  110. Console.WriteLine(tokenResponse.Json);
  111. //Perform the actual HTTP request
  112. using (var httpClient = new HttpClient())
  113. {
  114. httpClient.SetBearerToken(tokenResponse.AccessToken);
  115. var url = _configuration["RemoteServices:TaskScheduling:BaseUrl"] +
  116. "api/TaskScheduling/sample/authorized";
  117. var responseMessage = await httpClient.GetAsync(url);
  118. if (responseMessage.IsSuccessStatusCode)
  119. {
  120. var responseString = await responseMessage.Content.ReadAsStringAsync();
  121. Console.WriteLine("Result: " + responseString);
  122. }
  123. else
  124. {
  125. throw new Exception("Remote server returns error code: " + responseMessage.StatusCode);
  126. }
  127. }
  128. }
  129. }