gpt4 book ai didi

.net - Azure.管理.消费.模型

转载 作者:行者123 更新时间:2023-12-03 04:06:33 27 4
gpt4 key购买 nike

有没有一种方法可以像访问 Microsoft.Azure.Management.Fluent 一样访问 Azure.Management.Conclusion.Models?

当我执行以下代码时,我看不到 Azure.Management.Conclusion 是列表。我错过了什么吗?

var azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.Authenticate(credentials)
.WithDefaultSubscription();

最佳答案

根据我的测试,请引用以下代码

  1. 安装 SDK Microsoft.Azure.Management.Conclusion
Install-Package Microsoft.Azure.Management.Consumption -Version 3.0.2
  • Create a service principal with Azure CLI
  • az login
    az ad sp create-for-rbac --name "" --role "<you can use Billing Reader, Reader, Owner, or Contributor role>" --sdk-auth true
  • 代码
  • 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);

    }



    }

    enter image description here

    更多详情请参阅

    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

    关于.net - Azure.管理.消费.模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59583988/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com