When I try a azure rest api url on azure docs post man, I am able to get a json which has all the resource groups. https://learn.microsoft.com/en-us/rest/api/resources/resourcegroups/list#code-try-0a link
But I am trying it from a ASP.NET MVC Core C# application, I am receiving an empty array.
public async Task<ResourceGroupModel> GetResourceGroupStatus()
{
ResourceGroupModel resourceGroupModel = null;
try
{
string requestUrlString = iconfiguration.GetValue<string>("HealthSettings:AzureGetResourcesBySubscriptionURL");
string azureSubscription = iconfiguration.GetValue<string>("HealthSettings:AzureSubscription");
string clientId = iconfiguration.GetValue<string>("HealthSettings:ClientId");
string tenantId = iconfiguration.GetValue<string>("HealthSettings:TenantId");
string clientSecret = iconfiguration.GetValue<string>("HealthSettings:ClientSecret");
Uri requestUrl = new Uri(requestUrlString.Replace("{subscriptionId}", azureSubscription));
string token = await GetAccessToken(tenantId, clientId, clientSecret);
_httpClient.DefaultRequestHeaders.Remove("Authorization");
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var response = _httpClient.GetAsync(requestUrl);
if (response.Result.IsSuccessStatusCode)
{
var data = response.Result.Content.ReadAsStringAsync();
resourceGroupModel = ResourceGroupModel.FromJson(data.Result.ToString());
}
}
catch (Exception ex)
{
}
return resourceGroupModel;
}
Could you please help me to get rid of strange behavior of Azure REST API. Many thanks in advance. :)