Created
May 23, 2020 08:36
-
-
Save manoj-choudhari-git/e96c28d7ee19094bfccfdacdc23daea2 to your computer and use it in GitHub Desktop.
Weather Forecast Controller for the caller API which is making call to other web API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Authorize] | |
[ApiController] | |
[Route("[controller]")] | |
public class WeatherForecastController : ControllerBase | |
{ | |
private readonly string[] scopeRequiredByApi = new string[] { "caller-api" }; | |
private readonly ILogger<WeatherForecastController> _logger; | |
private readonly ITokenAcquisition _tokenAcquisition; | |
public WeatherForecastController(ILogger<WeatherForecastController> logger, ITokenAcquisition tokenAcquisition) | |
{ | |
_logger = logger; | |
_tokenAcquisition = tokenAcquisition; | |
} | |
[HttpGet] | |
public async Task<IEnumerable<WeatherForecast>> Get() | |
{ | |
HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi); | |
return await CallOtherApi(); | |
} | |
private async Task<dynamic> CallOtherApi() | |
{ | |
// scopes required to access the new weather forecast service | |
string[] scopes = new string[] { "user.read", "api://5e999e55-a661-4982-b897-965480492129/access_as_user" }; | |
// Get cached token | |
string accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(scopes); | |
HttpClient client = new HttpClient(); | |
// After the token has been returned by Microsoft Authentication Library (MSAL), | |
// add it to the HTTP authorization header before making the call | |
// to access the weather forecast service. | |
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | |
// Call the To Do list service. | |
string url = "https://localhost:44389/weatherforecast"; | |
HttpResponseMessage response = await client.GetAsync(url); | |
string content = await response.Content.ReadAsStringAsync(); | |
if (response.StatusCode == HttpStatusCode.OK) | |
{ | |
return JsonConvert.DeserializeObject<List<WeatherForecast>>(content); | |
} | |
throw new Exception("api call failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment