Cannot list SQL servers and SQL databases for each server in Azure using service principal

Kaustubh Patil 6 Reputation points
2022-01-05T23:30:52.817+00:00

I am trying to get a list of SQL servers and SQL databases using the SqlManagementClient from .NET SDK. I have the following requirement:

  • List the number of SQL servers in a single Azure Subscription
  • List the number of SQL Databases in each of the SQL Servers listed above

In order to do so, we are authenticating using service principal. Following is the snippet of the function that we are using to achieve this.

var azureCredentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
           Constants.ApplicationId,
           Constants.ClientSecret,
           Constants.TenantId,
           AzureEnvironment.AzureGlobalCloud)
    .WithDefaultSubscription(Constants.SubscriptionId);

RestClient restClient = RestClient.Configure()
    .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
    .WithCredentials(azureCredentials)
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Build();

SqlManagementClient sqlManagementClient = new SqlManagementClient(restClient);
sqlManagementClient.SubscriptionId = Constants.SubscriptionId;
var response = await SubscriptionUsagesOperationsExtensions.ListByLocationAsync(sqlManagementClient.SubscriptionUsages, Constants.Location);

Now the problem part. There are a number of SQL servers that are active under the subscription. Although the above returns an empty list. However if we use a bearer token and list the servers using the same REST API that the above method uses it returns valid results and accurate ones as well. It would be really helpful if you could point out what the issue is with regards to the above example.

Azure SQL Database
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,820 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Saurabh Sharma 23,796 Reputation points Microsoft Employee
    2022-01-14T20:09:36.657+00:00

    @Kaustubh Patil sorry for the delay. I have provided Reader permissions to the service principal on the subscription.

    Thanks
    Saurabh


  2. Kaustubh Patil 6 Reputation points
    2022-01-28T01:06:41.17+00:00

    As you can see from the discussion above the solution was to set the Reader permission on the subscription for the client being used to fetch the information. Once the permission is set the solution works just fine.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.