Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何获取Azure自动化客户端的证书_Asp.net Mvc_Azure_Asp.net Mvc 4_Azure Automation - Fatal编程技术网

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

Asp.net mvc 如何获取Azure自动化客户端的证书,asp.net-mvc,azure,asp.net-mvc-4,azure-automation,Asp.net Mvc,Azure,Asp.net Mvc 4,Azure Automation,我需要为Azure webhook创建Automion客户端 下面的代码是我为获取AutomationManagementClient值而编写的 var cert = new X509Certificate2(Convert.FromBase64String(ConfigurationManager.AppSettings["CertBase64String"])); var creds[![enter image description here][1]][1] = new Certif

我需要为Azure webhook创建Automion客户端

下面的代码是我为获取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值,因为我不知道从何处获取该值。 帮帮我

根据您的答案更新后,我收到此错误


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

准备:注册广告应用程序并为应用程序分配角色,更多详细信息请咨询Azure官方。之后,我们可以从Azure门户获取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;

如果您使用版本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;
}

我已经运行了上面的代码,但在参数System.ArgumentNullException:'值不能为null时出错。Parameter name:parameters.name'您使用的是Microsoft.Azure.Management.Automation的哪个版本,以及哪一行出现错误?您可以添加有关该参数的详细信息。您可以将参数设置为null,或者如果您有参数,则可以将参数定义为dictionary。我也更新了答案,详情请参考。我仍然得到相同的错误。我已经更新了我的问题,因为我添加了错误快照。请检查并帮助我。请查看我的最新答案。我还添加了webhook名称。名称=xxxx
 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"


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

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