user18093810
user18093810

Reputation: 1

Rest Call to Azure

I am trying to login to Azure with my c# code and was able to find something like this on the net.

Not sure what to pass in the scope array here and once I get the access token, how can I make the rest call ?

public static string getAccessToken(string[] scopes)
{
   var interactiveCredential = new InteractiveBrowserCredential();
   return interactiveCredential.GetToken(new Azure.Core.TokenRequestContext(scopes, null)).Token;
}

Upvotes: 0

Views: 391

Answers (1)

Amjad S.
Amjad S.

Reputation: 1241

First create a Azure AD Application:

Follow this link

Then get the Tenant ID , Client ID , Client Secret from the AD Application and use this class to query your azure subscription recourses.

class CustomLoginCredentials : ServiceClientCredentials
{
    //Variables
    private static string tenantId = "<Tenant ID goes here>";
    private static string clientId = "<Client ID goes here>";
    private static string clientSecret = "<Client Secret goes here>";
    private static string windowsURL = "https://login.windows.net/";
    private static string azureManagementURL = "https://management.azure.com/";


    private string AuthenticationToken { get; set; }

    public override void InitializeServiceClient<T>(ServiceClient<T> client)
    {
        var authenticationContext =
            new AuthenticationContext(windowsURL + tenantId);
        var credential = new ClientCredential(clientId, clientSecret);

        var result = authenticationContext.AcquireTokenAsync(azureManagementURL,
            clientCredential: credential).Result;

        AuthenticationToken = result.AccessToken;
    }
    public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
        await base.ProcessHttpRequestAsync(request, cancellationToken);
    }
}

Example:

private async void GetAzureResourcesConsumption()
        {
            var credentials = new CustomLoginCredentials();
            ConsumptionManagementClient client = new ConsumptionManagementClient(credentials);
            client.SubscriptionId = subscriptionId;

            var resources = await client.UsageDetails.ListAsync(null, null, null, top: NumberOfItems);
            var results = resources.ToList<UsageDetail>();
        } 

Do you mean to get access token?

private static string GetAuthorizationToken()
    {
        ClientCredential cc = new ClientCredential(ClientId, ServicePrincipalPassword);
        var context = new AuthenticationContext("https://login.windows.net/" + AzureTenantId);
        var result = context.AcquireTokenAsync("https://management.azure.com/", cc);
        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }

        return result.Result.AccessToken;
    }

Upvotes: 1

Related Questions