c# - 是否有用于 Azure 资源管理 API 调用的 C# sdk/包装器?

标签 c# api azure azure-resource-manager azure-sdk-.net

我想执行基于资源组的调用。例如: https://msdn.microsoft.com/en-us/library/azure/mt163572.aspx

Azure 管理库似乎没有此功能(除非我遗漏了某些内容)。是否有任何 SDK 或客户端包装器可以进行此类调用?

编辑: 高拉夫准确地指出了我所需要的。我将为人们提供扎实的帮助,并扩展我所做的工作,以帮助清除 Azure 资源管理 API 的浑水。

在应用程序的数据包管理器中执行以下操作: 安装包 Microsoft.Azure.Management.Resources -Pre 然后 安装包 Microsoft.Azure.Management.Compute -Pre 然后 安装包 Microsoft.IdentityModel.Clients.ActiveDirectory -Pre

按照此博客获取授权 header / token : https://msdn.microsoft.com/en-us/library/azure/dn722415.aspx

然后像这样调用新的 API(注意轻微的名称更改):

class Program
{
    static void Main(string[] args)
    {
        var token = GetAuthorizationHeader();
        var credential = new Microsoft.Rest.TokenCredentials(token);
        using (var client = new ComputeManagementClient(credential) { SubscriptionId = ConfigurationManager.AppSettings["subscriptionId"] })
        {
            var vms = client.VirtualMachines.ListAll();
        }
    }

    private static string GetAuthorizationHeader()
    {
        AuthenticationResult result = null;

        var context = new AuthenticationContext("https://login.windows.net/" + ConfigurationManager.AppSettings["tenantId"]);

        string clientId = ConfigurationManager.AppSettings["clientId"];
        string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
        ClientCredential clientCred = new ClientCredential(clientId, clientSecret);

        var thread = new Thread(() =>
        {
            result = context.AcquireToken(
              "https://management.core.windows.net/",
              clientCred);
        });

        thread.SetApartmentState(ApartmentState.STA);
        thread.Name = "AquireTokenThread";
        thread.Start();
        thread.Join();

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

        string token = result.AccessToken;
        return token;
    }
}

最佳答案

我相信您正在寻找的软件包是 Microsoft.Azure.Management.Resources 3.4.0-preview 。您可以在此处找到 Azure 资源管理器的完整源代码:https://github.com/Azure/azure-sdk-for-net/tree/master/src/ResourceManagement .

关于c# - 是否有用于 Azure 资源管理 API 调用的 C# sdk/包装器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35278231/

相关文章:

java - 使用 Retrofit 将 id 的 int 值转换为 String 时找不到资源

c++ - 在 C (Arduino IDE) 中将 API 链接消息解析为服务器

azure - 静默安装 Azure 部署代理

azure - 规则引擎在 Azure 上的流分析中意味着什么?

azure - 是什么原因导致 "invalid query definition, dataset is invalid or not supplied"使用 powershell 查询 Azure REST API?

c# - EF Core 总和日期与 where 语句内的时间跨度

c# - WPF - CommandBinding 到 F# 库中定义的命令不起作用

c# - DataContractJsonSerializer 设置值扩展点

c# - 断开连接后如何重新连接到 SQL Server?

node.js - 最佳实践建议 - Loopback API