【问题标题】:How to get certificate for Azure Automation Client如何获取 Azure 自动化客户端的证书
【发布时间】:2018-07-17 09:47:30
【问题描述】:

我需要为 Azure webhook 创建 automaion 客户端。

以下代码由我编写,用于获取 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 值,因为我不知道从哪里获得该值。 帮帮我...

根据您的回答更新后出现此错误。

【问题讨论】:

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


    【解决方案1】:

    “CertBase64String”将通过将该证书的指纹传递给以下函数来获得。

    internal static X509Certificate2 GetCertificateFromthumbPrint(String certThumbPrint) {
      X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    
      certStore.Open(OpenFlags.ReadOnly);
      //Find the certificate that matches the thumbprint.
      X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certThumbPrint, false);
      certStore.Close();
    
      //Get the first cert with the thumbprint
      X509Certificate2 cert = (certCollection.Count > 0) ? certCollection[0] : null;
      return cert;
    }
    

    【讨论】:

      【解决方案2】:

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

      准备:注册一个AD应用并为应用分配角色,更多细节请参考Azure官方tutorials。之后我们就可以从 Azure Portal 获取tenantId、appId、secretKey。

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

       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 版本

        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"
      
                         } 
                      });
      

      我自己测试了一下,它工作正常

      【讨论】:

      • 我已经运行了上面的代码,但在“参数”处出现错误,因为“System.ArgumentNullException: 'Value cannot be null. Parameter name: parameters.Name'”
      • 您使用的是哪个版本的 Microsoft.Azure.Management.Automation,哪一行出现错误?您可以添加有关此的详细信息。
      • 您可以设置Parameters = null 或者如果您有参数,您可以将参数定义为字典。我也更新了答案,详情请参考。
      • 我仍然遇到同样的错误。我已经更新了我的问题,因为我添加了错误快照。请检查并帮助我
      • 请看我更新的答案。我还添加了 webhook 名称。 Name = "xxxx"
      猜你喜欢
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 2014-07-07
      • 2014-11-01
      • 2019-05-02
      相关资源
      最近更新 更多