using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; namespace OidcSamples.TaxApp.Pages { public enum VehicleType { Sedan = 0, SUV = 1, Pickup = 2 } public class Vehicle { public long Id { get; set; } public string OwnerId { get; set; } public string Model { get; set; } public string Color { get; set; } public string LicensePlate { get; set; } public VehicleType Type { get; set; } } public class RealEstate { public int Id { get; set; } public double Area { get; set; } public string Address { get; set; } public string Citizen_upn { get; set; } } [Authorize] //(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)] public class IndexModel : PageModel { private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; public IndexModel(ILogger logger, IHttpClientFactory httpClientFactory) { _logger = logger; _httpClientFactory = httpClientFactory; } public string FirstName { get; set; } public string LastName { get; set; } public decimal TotalTax { get; set; } public List Vehicles { get; set; } public List RealEstate { get; set; } public async Task OnGet() { var tpClient = _httpClientFactory.CreateClient("TP-APIClient"); var tpResponse = await tpClient.GetAsync($"/api/vehicles"); Vehicles = await GetVehicles(); RealEstate = await GetRealEstate(); TotalTax = Vehicles.Sum(v => 20_000) + RealEstate.Sum(r => 100_000); FirstName = User.Claims.First(c => c.Type == "given_name").Value; LastName = User.Claims.First(c => c.Type == "family_name").Value; } private async Task> GetVehicles() { var tpClient = _httpClientFactory.CreateClient("TP-APIClient"); var tpResponse = await tpClient.GetAsync($"/api/vehicles"); if (tpResponse.IsSuccessStatusCode) { using (var responseStream = await tpResponse.Content.ReadAsStreamAsync()) { return await JsonSerializer.DeserializeAsync>(responseStream, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } } else { throw new System.Exception("Problem accessing Traffic Police the API"); } } private async Task> GetRealEstate() { var tpClient = _httpClientFactory.CreateClient("RE-APIClient"); var tpResponse = await tpClient.GetAsync($"/real-estate"); if (tpResponse.IsSuccessStatusCode) { using (var responseStream = await tpResponse.Content.ReadAsStreamAsync()) { return await JsonSerializer.DeserializeAsync>(responseStream, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } } else { throw new System.Exception("Problem accessing the Real Estate API"); } } } }