I'm using Azure Authentication. When my API is called, it returns an HTML page instead of the JSON I expect. It seems the call doesn't wait until the user is properly authenticated, so I end up with an HTML response and a JSON deserialization error.
Below is my code:
private async Task<List<MyData>> GetSomeDataAsync(int productId, int sizeId, DateTime? dateFrom)
{
var baseUrl = ConfigurationManager.AppSettings["MyBaseUrl"];
var url = $"{baseUrl}/api/SomeEndpoint/{productId}";
if (dateFrom.HasValue)
{
url += $"?dateFrom={dateFrom.Value:yyyy-MM-dd}";
}
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return null;
}
var json = await response.Content.ReadAsStringAsync();
var apiResponse = JsonConvert.DeserializeObject<MyApiResponse>(json);
if (apiResponse?.MyDataList != null)
{
return apiResponse.MyDataList
.Where(x => x.SizeId == sizeId)
.ToList();
}
}
return null;
}
How can I ensure that my request waits until the user is fully authenticated and returns valid JSON instead of an HTML login page?
Any guidance or best practices for handling Azure Authentication in this scenario would be greatly appreciated. Thank you!