asp.net-mvc - 如何获取 Azure 自动化客户端的证书

标签 asp.net-mvc azure asp.net-mvc-4 azure-automation

我需要为 Azure webhook 创建自动化客户端。

以下代码是我编写的,用于获取 AutomationManagementClient 值。

var cert = new X509Certificate2(Convert.FromBase64String(ConfigurationManager.AppSettings["CertBase64String"]));

  var creds[![enter image description here][1]][1] = new CertificateCloudCredentials(ConfigurationManager.AppSettings["SubscriptionId"], cert);

  AutomationManagementClient automationManagementClient = new AutomationManagementClient(creds);

我需要该证书字符串,即 CertBase64String 值,因为我不知道从哪里获得该值。 帮帮我...

根据您的回答更新后我收到此错误。

enter image description here

最佳答案

如果你想创建自动化客户端,我建议你尝试使用ARM方式来操作自动化。以下是我这边可以正常运行的演示代码。

准备:注册AD应用并为应用分配角色,更多详情请引用Azure官方tutorials 。之后我们可以从Azure Portal获取tenantId、appId、secretKey。

我们可以使用以下代码来获取 token

 var tenantId = "tenantId";
 var context = new AuthenticationContext($"https://login.windows.net/{tenantId}");
 var clientId = "application Id";
 var clientSecret = "client secret";
 var resourceGroup = "resource group";
 var automationAccount = "automationAccount";
 var subscriptionId = "susbscriptionId";
 var token = context.AcquireTokenAsync(
                "https://management.azure.com/",
                new ClientCredential(clientId, clientSecret)).Result.AccessToken;

如果您使用Microsoft.Azure.Management.Automation版本 <= 2.0.4 请尝试以下代码。

  var automationClient = new AutomationManagementClient(new TokenCloudCredentials(subscriptionId,token));
  var webhook = automationClient.Webhooks.CreateOrUpdate(resourceGroup, automationAccount,new WebhookCreateOrUpdateParameters
                {
                   Properties =  new WebhookCreateOrUpdateProperties
                   {
                       ExpiryTime = DateTimeOffset.Now.AddDays(1),
                       IsEnabled = false,
                       Parameters = parameters,
                       Runbook = new RunbookAssociationProperty
                       {
                           Name = "xxxx"
                       },
                       Name = "xxxx",
                       Uri = "https://xxxx.xx"

                   } 
                });

如果使用Microsoft.Azure.Management.Automation Version 3.0.0-preview ,请尝试以下案例。

 var automationClient = new AutomationClient(new TokenCredentials(token)) {SubscriptionId = subscriptionId};
 var webhook = automationClient.Webhook.CreateOrUpdate(resourceGroup, automationAccount, "webhookName",
  new WebhookCreateOrUpdateParameters
   {
      ExpiryTime = DateTime.Now.AddDays(1),
      IsEnabled = false,
      Parameters = parameters,
      Name = "xxxxx",
      Runbook = new RunbookAssociationProperty
      {
           Name = "xxxxx"
      },
      Uri = "https://xxx.xxx"


  });

更新:

您可以设置Parameters = null,或者如果您有参数,您可以将参数定义为字典。另请在代码中添加Name = "xxxx"

var parameters = new Dictionary<string, string> {{"test", "test"}};

var webhook = automationClient.Webhooks.CreateOrUpdate(resourceGroup, automationAccount,new WebhookCreateOrUpdateParameters
                {
                   Properties =  new WebhookCreateOrUpdateProperties
                   {
                       ExpiryTime = DateTimeOffset.Now.AddDays(1),
                       IsEnabled = false,
                       Parameters = parameters,
                       Runbook = new RunbookAssociationProperty
                       {
                           Name = "xxxx"
                       },
                       Name = "xxxx",
                       Uri = "https://xxxx.xx"

                   } 
                });

我在我这边测试了一下,它工作正常

enter image description here

关于asp.net-mvc - 如何获取 Azure 自动化客户端的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48658126/

相关文章:

c# - 访问内部 API Controller

c# - C# MVC 中 Controller 到 Controller 的通信

c# - 禁用超时时 SqlBulkCopy 到 Azure 超时

asp.net-mvc - 如何调试部署在 Azure 中的 ASPNET Core MVC 应用程序

c# - 枚举本地化

asp.net - Beginform 不发送 View 模型,也没有到达我的 httppost 方法

c# - 在 ASP.net MVC 5 中为模型创建隐藏字段时出错

azure - 有没有办法使用 Bicep 在 Microsoft.DBforPostgreSQL/servers 中设置 "Allow access to Azure services"?

asp.net-mvc - MVC4 简单成员资格 'The Provider encountered an unknown error.'

c# - ASP.NET MVC 使用 Dapper 管理 SQLConnection