From 9659b7bc8c3c6b7ff42d48a1d3ba63ea5dc32d79 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 3 Feb 2025 09:53:13 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=B4=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.Client/Helpers/ApiTokenHelper.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/DD.Persistence.Client/Helpers/ApiTokenHelper.cs b/DD.Persistence.Client/Helpers/ApiTokenHelper.cs index 4a46016..3487dcb 100644 --- a/DD.Persistence.Client/Helpers/ApiTokenHelper.cs +++ b/DD.Persistence.Client/Helpers/ApiTokenHelper.cs @@ -9,12 +9,12 @@ using System.Text.Json; namespace DD.Persistence.Client.Helpers; /// -/// , api-token +///  Класс, позволяющий генерировать api-token /// public static class ApiTokenHelper { /// - /// + /// Метод авторизации /// /// /// @@ -30,14 +30,14 @@ public static class ApiTokenHelper var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get() ?? string.Empty; var jwtToken = needUseKeyCloak - ? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl) + ? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl, httpClient) : authUser.CreateDefaultJwtToken(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); } /// - /// jwt- + /// Авторизация через собственный jwt-токен /// /// /// @@ -68,14 +68,13 @@ public static class ApiTokenHelper } /// - /// jwt- keycloak + /// Авторизация через jwt-токен keycloak /// /// /// /// - private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl) + private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl, HttpClient httpClient) { - var sharedClient = new HttpClient(); var parameters = new Dictionary { { "username", authUser.Username }, { "password", authUser.Password }, @@ -84,7 +83,7 @@ public static class ApiTokenHelper }; var encodedContent = new FormUrlEncodedContent(parameters); - using HttpResponseMessage response = sharedClient.PostAsync(keycloakGetTokenUrl, encodedContent).GetAwaiter().GetResult(); + using HttpResponseMessage response = httpClient.PostAsync(keycloakGetTokenUrl, encodedContent).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode == true) {