最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论