我需要为 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 值,因为我不知道从哪里获得该值。 帮帮我...
根据您的回答更新后我收到此错误。
最佳答案
如果你想创建自动化客户端,我建议你尝试使用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"
}
});
我在我这边测试了一下,它工作正常
关于asp.net-mvc - 如何获取 Azure 自动化客户端的证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48658126/