c# - Azure Authentication: API Returning HTML Instead of JSON - Stack Overflow

I'm using Azure Authentication. When my API is called, it returns an HTML page instead of the JSON

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745089792a4610624.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信