gpt4 book ai didi

azure - 如何使用最新的 SDK 创建或更新证书

转载 作者:行者123 更新时间:2023-12-03 06:15:27 25 4
gpt4 key购买 nike

有谁知道SDK相当于this REST API吗?是?不幸的是,SDK 的文档记录太少了——经过整整两天的挖掘——我终于不得不问了。

我之前的研究和尝试过的事情很详细here .

最佳答案

要使用 SDK 创建或更新证书,您可以引用此 Github sample 最新 Azure.ResourceManager.AppService .Net 示例

代码来自github sample上图:-


// Create Or Update Certificate
[NUnit.Framework.Test]
[NUnit.Framework.Ignore("Only verifying that the sample builds")]
public async Task CreateOrUpdate_CreateOrUpdateCertificate()
{
// Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/CreateOrUpdateCertificate.json
// this example is just showing the usage of "Certificates_CreateOrUpdate" operation, for the dependent resources,
they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to
https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "xxxxsubidxxxx";
string resourceGroupName = "rgname";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this AppCertificateResource
AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();

// invoke the operation
string name = "certname";
AppCertificateData data = new AppCertificateData(new AzureLocation("East US"))
{
Password = "<password>",
HostNames = { "ServerCert" },
};
ArmOperation<AppCertificateResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, name, data);
AppCertificateResource result = lro.Value;

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
AppCertificateData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}
}
}

管理应用服务证书的完整代码:-

using Azure.Core; using Azure.Identity; using Azure.ResourceManager;
using Azure.ResourceManager.AppService; using
Azure.ResourceManager.Resources; using NUnit;



namespace Azure.ResourceManager.AppService.Samples {
public partial class Sample_AppCertificateCollection
{
static void Main(string[] args)
{
}
// List Certificates by resource group
[NUnit.Framework.Test]
[NUnit.Framework.Ignore("Only verifying that the sample builds")]
public async Task GetAll_ListCertificatesByResourceGroup()
{
// Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/ListCertificatesByResourceGroup.json
// this example is just showing the usage of "Certificates_ListByResourceGroup" operation, for the dependent
resources, they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to
https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
string resourceGroupName = "siliconrg";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this AppCertificateResource
AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();

// invoke the operation and iterate over the result
await foreach (AppCertificateResource item in collection.GetAllAsync())
{
// the variable item is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
AppCertificateData resourceData = item.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}

Console.WriteLine($"Succeeded");
}

// Get Certificate
[NUnit.Framework.Test]
[NUnit.Framework.Ignore("Only verifying that the sample builds")]
public async Task Get_GetCertificate()
{
// Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/GetCertificate.json
// this example is just showing the usage of "Certificates_Get" operation, for the dependent resources, they will
have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to
https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
string resourceGroupName = "siliconrg";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this AppCertificateResource
AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();

// invoke the operation
string name = "certsilicon";
AppCertificateResource result = await collection.GetAsync(name);

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
AppCertificateData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}

// Get Certificate
[NUnit.Framework.Test]
[NUnit.Framework.Ignore("Only verifying that the sample builds")]
public async Task Exists_GetCertificate()
{
// Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/GetCertificate.json
// this example is just showing the usage of "Certificates_Get" operation, for the dependent resources, they will
have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to
https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
string resourceGroupName = "siliconrg";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this AppCertificateResource
AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();

// invoke the operation
string name = "certsilicon";
bool result = await collection.ExistsAsync(name);

Console.WriteLine($"Succeeded: {result}");
}

// Create Or Update Certificate
[NUnit.Framework.Test]
[NUnit.Framework.Ignore("Only verifying that the sample builds")]
public async Task CreateOrUpdate_CreateOrUpdateCertificate()
{
// Generated from example definition: specification/web/resource-manager/Microsoft.Web/stable/2021-02-01/examples/CreateOrUpdateCertificate.json
// this example is just showing the usage of "Certificates_CreateOrUpdate" operation, for the dependent resources,
they will have to be created separately.

// get your azure access token, for more details of how Azure SDK get your access token, please refer to
https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "0151c365-f598-44d6-b4fd-e2b6e97cb2a7";
string resourceGroupName = "siliconrg";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId,
resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this AppCertificateResource
AppCertificateCollection collection = resourceGroupResource.GetAppCertificates();

// invoke the operation
string name = "certsilicon";
AppCertificateData data = new AppCertificateData(new AzureLocation("East US"))
{
Password = "password",
HostNames = { "ServerCert" },
};
ArmOperation<AppCertificateResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, name, data);
AppCertificateResource result = lro.Value;

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
AppCertificateData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}
} }

输出:-

enter image description here

关于azure - 如何使用最新的 SDK 创建或更新证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76299245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com