I have a service principal which has access to all tenants and its resources in my organization.
The client_id client_secret and tenant_id
are used in a .Net Core 6 API
and following function is used to get an access token of that service principal:
public string getAccessToken(string tenantID)
{
var _config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
var scopes = new string[] { _config["Azure:vm-executer:scope"]! };
var confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(_config["Azure:vm-executer:client_id"])
.WithTenantId(tenantID)
.WithClientSecret(_config["Azure:vm-executer:client_secret"])
.WithAuthority($"https://login.microsoftonline.com/{tenantID}/oauth2/v2.0/token")
.Build();
var result = confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
return result.Result.AccessToken;
}
I used that access token successfully to make requests to the Azure REST API to
- list subscriptions with
https://management.azure.com/subscriptions?api-version=2020-01-01
- list virtual machines with
https://management.azure.com/subscriptions/{subscriptionID}/providers/Microsoft.Compute/virtualMachines?api-version=2022-11-01
I did this all in postman and it works really well!
However, now I want to do from inside my .Net 6 API.
My goal is to specifically return the response postman gave me for a specific request.
So for example when I want to list the subscriptions, then it should have the same response from postman like this:
So far I have implemented this function using the RestClient but I’m stuck and am in need for help.
public SubscriptionResource getSubscriptionID(string accessToken)
{
var uri = "https://management.azure.com/subscriptions?api-version=2020-01-01";
var client = new RestClient();
var request = new RestRequest(uri, Method.Get);
request.AddHeader("Authorization", $"Bearer {accessToken}");
RestResponse response = client.Execute(request);
return response;
}
I have read about the Azure SDK for .net
and tried to use the Azure.ResourceManager
package but without success.
2
Answers
While the previous answer is completely right and fine, I solved my problem by using the
Azure SDK for .Net
. With that I am able to retrieve all information I'm getting when calling the Azure REST API using postman. Now I can use the dot notation and resource Objects to access its attributes.Here is an example code of how to get the subscriptionID as well as other resources like the virtual machines inside resource groups.
I tried to reproduce the same in my environment and got below results:
I used below c# code and got access token with list of subscriptions successfully in response like this:
Response:
To confirm that, I used the above access token in Postman and got same results like below:
Response: