{"id":38369,"date":"2020-01-29T09:14:49","date_gmt":"2020-01-29T16:14:49","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/premier-developer\/?p=38369"},"modified":"2020-01-27T09:33:41","modified_gmt":"2020-01-27T16:33:41","slug":"accessing-azure-resource-consumption-data-using-net","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/accessing-azure-resource-consumption-data-using-net\/","title":{"rendered":"Accessing Azure Resource Consumption Data Using .NET"},"content":{"rendered":"<p>App Dev Manager <a href=\"https:\/\/www.linkedin.com\/in\/kyleljohnson\/\">Kyle Johnson<\/a> shares a look at tracking resource consumption with .NET.<\/p>\n<hr \/>\n<p>I don\u2019t know about you but every time I call a web service in my application, I\u2019m nervous about going over my limit. Now I know they are ways you can protect yourself from spending too much like budgets which you can setup in the portal but I thought it would be helpful to show you how you can access your Azure resource consumption data via .NET. Having access to this data at the code level can allow ways to do other things if a budget limit is reached for a particular resource.<\/p>\n<p>You need a Service Principal If you don\u2019t already have one. You can use the Azure portal to create the Service Principal. <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/develop\/howto-create-service-principal-portal\">How to: Use the portal to create an Azure AD application and service principal that can access resources<\/a><\/p>\n<p>There are a few things you need:<\/p>\n<ol>\n<li>Subscription ID \u2013 The subscription id of the subscription you want to access the consumption data for<\/li>\n<li>Tenant ID \u2013 Tenant ID of your Service Principal.<\/li>\n<li>Client ID \u2013 Client ID of your Service Principal.<\/li>\n<li>Client Secret \u2013 Client Secret of your Service Principal.<\/li>\n<\/ol>\n<p>The Tenant ID, Client ID and Client Secret are used to authenticate you.<\/p>\n<p>Now that we have all the data we need, we can now get to writing some code.<\/p>\n<p>I\u2019m using the Microsoft.Rest.ServiceClientCredentials class to authenticate to Azure and obtain a credentials object.<\/p>\n<pre class=\"lang:c# decode:true\">class CustomLoginCredentials : ServiceClientCredentials\r\n{\r\n    \/\/Variables\r\n    private static string tenantId = \"&lt;Tenant ID goes here&gt;\";\r\n    private static string clientId = \"&lt;Client ID goes here&gt;\";\r\n    private static string clientSecret = \"&lt;Client Secret goes here&gt;\";\r\n    private static string windowsURL = \"https:\/\/login.windows.net\/\";\r\n    private static string azureManagementURL = \"https:\/\/management.azure.com\/\";\r\n\r\n\r\n    private string AuthenticationToken { get; set; }\r\n\r\n    public override void InitializeServiceClient&lt;T&gt;(ServiceClient&lt;T&gt; client)\r\n    {\r\n        var authenticationContext =\r\n            new AuthenticationContext(windowsURL + tenantId);\r\n        var credential = new ClientCredential(clientId, clientSecret);\r\n\r\n        var result = authenticationContext.AcquireTokenAsync(azureManagementURL,\r\n            clientCredential: credential).Result;\r\n\r\n        AuthenticationToken = result.AccessToken;\r\n    }\r\n    public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n    {\r\n        request.Headers.Authorization = new AuthenticationHeaderValue(\"Bearer\", AuthenticationToken);\r\n        await base.ProcessHttpRequestAsync(request, cancellationToken);\r\n    }\r\n}\r\n<\/pre>\n<p>Once I have a client credentials object I use the object to access the consumption data in Azure per resource in a given subscription.<\/p>\n<pre class=\"lang:c# decode:true\">private async void GetAzureResourcesConsumption()\r\n        {\r\n            var credentials = new CustomLoginCredentials();\r\n            ConsumptionManagementClient client = new ConsumptionManagementClient(credentials);\r\n            client.SubscriptionId = subscriptionId;\r\n\r\n            var resources = await client.UsageDetails.ListAsync(null, null, null, top: NumberOfItems);\r\n            var results = resources.ToList&lt;UsageDetail&gt;();\r\n        }\r\n<\/pre>\n<p><img decoding=\"async\" width=\"1714\" height=\"723\" class=\"wp-image-38370\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-47.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-47.png 1714w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-47-300x127.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-47-1024x432.png 1024w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-47-768x324.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2020\/01\/word-image-47-1536x648.png 1536w\" sizes=\"(max-width: 1714px) 100vw, 1714px\" \/><\/p>\n<p>For a detailed exploration of techniques to manage billing and cost management, see <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/cost-management-billing\/manage\/getting-started\">Prevent unexpected charges with Azure billing and cost management<\/a> which details a variety of techniques to help track your Azure consumption.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought it would be helpful to show you how you can access your Azure resource consumption data via .NET. Having access to this data at the code level can allow ways to do other things if a budget limit is reached for a particular resource.<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[80,25],"tags":[24,8710,3],"class_list":["post-38369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-azure","tag-azure","tag-azure-consumption","tag-team"],"acf":[],"blog_post_summary":"<p>I thought it would be helpful to show you how you can access your Azure resource consumption data via .NET. Having access to this data at the code level can allow ways to do other things if a budget limit is reached for a particular resource.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=38369"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/38369\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=38369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=38369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=38369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}