Calling Azure REST API from a windows .Net Core console application

David Downing 701 Reputation points
2020-02-12T21:55:43.587+00:00

Now that MF auth is required, I can't seem to figure out how to make an Azure REST API call from a console application (.Net Core). I've tried multiple samples, however none of them seem to work.

        string authContextURL = "https://login.windows.net/" + TenantID;
        var authenticationContext = new AuthenticationContext(authContextURL);
        var credential = new ClientCredential(ClientID, ClientSecret);
        var result = authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential).GetAwaiter().GetResult();
        if (result == null)
        {
            throw new InvalidOperationException("Failed to obtain the JWT token");
        }
        string token = result.AccessToken;
        return token;

Error: "The received access token is not valid: at least one of the claims 'puid' or 'altsecid' or 'oid' should be present. If you are accessing as application please make sure service principal is properly created in the tenant."

In addition, setting up the Service Principal (or whatever is required) has also changed.

Is there a walk-through and code for getting the Service Principal setup and a console application able to call Azure REST APIs?

Thank you.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,465 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Saurabh Sharma 23,676 Reputation points Microsoft Employee
    2020-02-12T22:49:54.037+00:00

    You can refer to the Scenario: Daemon application that calls web APIs to learn on building a daemon application which calls a Web API including the OAuth Client Credentials grant. You also need to register your application with no reply uri as the client credentials flow doesn't use it. Please refer to the Daemon app that calls web APIs - app registration documentation for application registration details.
    You can also find a GitHub sample here which shows to use a console application to call a Microsoft graph using .NET core 2.2.

    1 person found this answer helpful.
    0 comments No comments

  2. David Downing 701 Reputation points
    2020-02-13T20:01:16.203+00:00

    I'm able to get a token, however I'm getting "Unauthorized". The sample doesn't break down what is needed in terms of Azure REST API permissions.

    I just keep adding more and more permissions, but surely someone know what is needed to call Azure REST APIs.

    Thank you.

    2914-appregpermissions.png