首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure.Management.Consumption.Models

Azure.Management.Consumption.Models
EN

Stack Overflow用户
提问于 2020-01-03 19:05:51
回答 1查看 384关注 0票数 1

是否有一种像访问Azure.Management.Consumption.Models时一样访问Microsoft.Azure.Management.Fluent的方法?

当我执行以下代码时,我看不到Azure.Management.Consumption是列表。我是不是遗漏了什么?

代码语言:javascript
复制
var azure = Microsoft.Azure.Management.Fluent.Azure
    .Configure()
    .Authenticate(credentials)
    .WithDefaultSubscription();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-06 03:17:37

根据我的测试,请参考以下代码

  1. 安装SDK Microsoft.Azure.Management.Consumption
代码语言:javascript
复制
Install-Package Microsoft.Azure.Management.Consumption -Version 3.0.2
  1. 使用Azure CLI创建服务主体
代码语言:javascript
复制
az login
az ad sp create-for-rbac --name "" --role "<you can use  Billing Reader, Reader, Owner, or Contributor role>" --sdk-auth true
  1. 代码
代码语言:javascript
复制
private static string subscriptionId = "";
        private const int NumberOfItems = 100;
        public static async Task testCosAsync() {
            var cred = new CustomLoginCredentials();
            ConsumptionManagementClient client = new ConsumptionManagementClient(cred);
            Console.WriteLine(client.ApiVersion);
            client.SubscriptionId = subscriptionId;
            
            Console.WriteLine("---------------1");
            var r = await client.UsageDetails.ListAsync(null,null,null,top: NumberOfItems);
            Console.WriteLine("---------------2");
            var results = r.ToList<UsageDetail>();

            foreach (var result in results) {
                Console.WriteLine("Name:" + result.Name);
            
            }



        }
    }

    class CustomLoginCredentials : ServiceClientCredentials {
        private static string tenantId = "";
        private static string clientId = "you sp app id";
        private static string cert = "your sp password";
        private string AuthenticationToken { get; set; }
        public override void InitializeServiceClient<T>(ServiceClient<T> client)
        {
            var authenticationContext =
                new AuthenticationContext("https://login.windows.net/"+tenantId);
            var credential = new ClientCredential(clientId: clientId, clientSecret: cert);

            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/",
                clientCredential: credential).Result;

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }

            AuthenticationToken = result.AccessToken;
        }
        public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (AuthenticationToken == null)
            {
                throw new InvalidOperationException("Token Provider Cannot Be Null");
            }



            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
          

           
            await base.ProcessHttpRequestAsync(request, cancellationToken);

        }



    }

有关更多详情,请参阅

https://learn.microsoft.com/en-us/azure/billing/billing-consumption-api-overview

https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/consumption/Microsoft.Azure.Management.Consumption

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59583988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档