private async Task <IActionResult> CallApiWithToken(string accessToken) { var uri = new Uri("http://localhost:5003/patients/123"); var client = _httpClientFactory.CreateWithAccessToken(uri, accessToken); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.GetAsync(uri); if (response.IsSuccessStatusCode) { ViewBag.PatientDataResponse = JsonConvert.DeserializeObject <PatientDataResponse>(await response.Content.ReadAsStringAsync()); return(View("Patient")); } if (response.StatusCode == HttpStatusCode.Forbidden) { ViewBag.ErrorMessage = $"Received 403 Forbidden when calling: {uri}"; return(View("Patient")); } throw new Exception($"Error received: {response.StatusCode} when trying to contact remote server: {uri}"); }
public FabricAuthorizationService(IHttpClientFactory clientFactory) { _client = clientFactory?.CreateWithAccessToken(UriBase, string.Empty) ?? throw new ArgumentNullException(nameof(clientFactory)); _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); }