Удаление restsharp и использование обычного httpClient #22
@ -19,7 +19,7 @@ public static class ApiTokenHelper
|
|||||||
/// <param name="httpClient"></param>
|
/// <param name="httpClient"></param>
|
||||||
/// <param name="configuration"></param>
|
/// <param name="configuration"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task Authorize(this HttpClient httpClient, IConfiguration configuration)
|
public static void Authorize(this HttpClient httpClient, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var authUser = configuration
|
var authUser = configuration
|
||||||
.GetSection(nameof(AuthUser))
|
.GetSection(nameof(AuthUser))
|
||||||
@ -30,7 +30,7 @@ public static class ApiTokenHelper
|
|||||||
var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get<string>() ?? string.Empty;
|
var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get<string>() ?? string.Empty;
|
||||||
|
|
||||||
var jwtToken = needUseKeyCloak
|
var jwtToken = needUseKeyCloak
|
||||||
? await authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl)
|
? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl)
|
||||||
: authUser.CreateDefaultJwtToken();
|
: authUser.CreateDefaultJwtToken();
|
||||||
|
|
||||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
||||||
@ -73,7 +73,7 @@ public static class ApiTokenHelper
|
|||||||
/// <param name="authUser"></param>
|
/// <param name="authUser"></param>
|
||||||
/// <param name="keycloakGetTokenUrl"></param>
|
/// <param name="keycloakGetTokenUrl"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static async Task<string> CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl)
|
private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl)
|
||||||
{
|
{
|
||||||
var sharedClient = new HttpClient();
|
var sharedClient = new HttpClient();
|
||||||
var parameters = new Dictionary<string, string> {
|
var parameters = new Dictionary<string, string> {
|
||||||
@ -84,11 +84,11 @@ public static class ApiTokenHelper
|
|||||||
};
|
};
|
||||||
var encodedContent = new FormUrlEncodedContent(parameters);
|
var encodedContent = new FormUrlEncodedContent(parameters);
|
||||||
|
|
||||||
using HttpResponseMessage response = await sharedClient.PostAsync(keycloakGetTokenUrl, encodedContent);
|
using HttpResponseMessage response = sharedClient.PostAsync(keycloakGetTokenUrl, encodedContent).GetAwaiter().GetResult();
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode == true)
|
if (response.IsSuccessStatusCode == true)
|
||||||
{
|
{
|
||||||
var data = await response.Content.ReadAsStreamAsync();
|
var data = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
|
||||||
var token = JsonSerializer.Deserialize<JwtToken>(data)!;
|
var token = JsonSerializer.Deserialize<JwtToken>(data)!;
|
||||||
return token.AccessToken;
|
return token.AccessToken;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ namespace DD.Persistence.IntegrationTests
|
|||||||
public HttpClient CreateClient(string name)
|
public HttpClient CreateClient(string name)
|
||||||
{
|
{
|
||||||
var client = factory.CreateClient();
|
var client = factory.CreateClient();
|
||||||
client.Authorize(configuration).GetAwaiter().GetResult();
|
client.Authorize(configuration);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user