From 14be26868c797684b898396bcb434bd17b862022 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Tue, 20 Aug 2024 15:10:58 +0800 Subject: [PATCH] fix offline token --- .../Controllers/OAuth2Controller.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Src/Dlhis.Middle/Controllers/OAuth2Controller.cs b/Src/Dlhis.Middle/Controllers/OAuth2Controller.cs index 38170835..5b448c0d 100644 --- a/Src/Dlhis.Middle/Controllers/OAuth2Controller.cs +++ b/Src/Dlhis.Middle/Controllers/OAuth2Controller.cs @@ -1,5 +1,6 @@ using System.Text; using Dlhis.Middle.Request; +using Dlhis.Middle.Services; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; @@ -14,11 +15,13 @@ public class OAuth2Controller : ControllerBase { private readonly IHttpClientFactory _httpClientFactory; private readonly IConfiguration _configuration; + private readonly IHealthCheckService _healthCheckService; - public OAuth2Controller(IHttpClientFactory httpClientFactory, IConfiguration configuration) + public OAuth2Controller(IHttpClientFactory httpClientFactory, IConfiguration configuration, IHealthCheckService healthCheckService) { _httpClientFactory = httpClientFactory; _configuration = configuration; + _healthCheckService = healthCheckService; } [HttpPost("clientgrant")] @@ -27,14 +30,7 @@ public class OAuth2Controller : ControllerBase var authority = _configuration.GetSection("Authority").Value; var httpClient = _httpClientFactory.CreateClient("face"); - try - { - var content = new StringContent($"grant_type=client_credentials&client_id={request.ClientId}&client_secret={request.ClientSecret}&scope={request.Scope}", Encoding.UTF8, "application/x-www-form-urlencoded"); - var response = await httpClient.PostAsync($"{authority}/oauth2/connect/token", content); - var result = await response.Content.ReadAsStringAsync(); - return JObject.Parse(result); - } - catch (Exception e) + if (_healthCheckService.GetIsOffline()) { return new { @@ -44,5 +40,10 @@ public class OAuth2Controller : ControllerBase scope = "yikatong" }; } + + var content = new StringContent($"grant_type=client_credentials&client_id={request.ClientId}&client_secret={request.ClientSecret}&scope={request.Scope}", Encoding.UTF8, "application/x-www-form-urlencoded"); + var response = await httpClient.PostAsync($"{authority}/oauth2/connect/token", content); + var result = await response.Content.ReadAsStringAsync(); + return JObject.Parse(result); } } \ No newline at end of file