Search code examples
oauth-2.0azure-authenticationazure-ad-b2cazure-resource-graph

Fetch billing properties of Azure b2c tenant from API


I have been working on b2c from a couple of weeks. I created many resources like applications, users, user flows, policies.

I want to know the billing details of this particular tenant.

I found this query where I can get charges list within a scope:

GET https://management.azure.com/{scope}/providers/Microsoft.Consumption/charges?api-version=2021-10-01

Any way to filter this query so that it shows details only related to b2c tenant

But I'm not sure whether this is correct or not. Can anyone with this domain knowledge help me out whether I'm on right path or any modifications needed?


Solution

  • To get billing properties of your B2C tenant, you need to use query like below:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AzureActiveDirectory/b2cDirectories/{resourceName}?api-version=2019-01-01-preview
    

    I tried to reproduce the same in my environment and got below results:

    I registered one Azure AD application and added API permissions as below:

    enter image description here

    Make sure to enable tokens if you are using implicit flow to acquire access token like below:

    enter image description here

    To get the access token, I made Authorization request in browser like below:

    https://login.microsoftonline.com/<tenant_ID>/oauth2/v2.0/authorize?
    client_id=74b2a1ba-xxxx-4xxc-axx1-c2xxxxxxxx
    &response_type=token
    &redirect_uri=https://jwt.ms
    &scope=https://management.azure.com/user_impersonation
    &response_mode=fragment
    &state=12345
    &nonce=678910
    

    When I ran the above request, I got the token in the address bar as below:

    enter image description here

    I executed the query via Postman and got the billing properties of my B2C tenant successfully like below:

    GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AzureActiveDirectory/b2cDirectories/{b2ctenantdomain}?api-version=2019-01-01-preview
    

    Response:

    enter image description here

    UPDATE

    To get the details like the resource group in which B2C tenant is created, you can run below PowerShell command:

    Get-AzResource -Name tenantname.onmicrosoft.com
    

    Response:

    enter image description here