使用Azure SDK将PFX SSL证书上载到Azure App Service Web App

xn1cxnb4  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(102)

我正在尝试使用Azure SDK.NET将PFX SSL证书上载到Azure App Service Web App。我有this question,但它使用了已弃用的Microsoft. Azure. Management. Fluent包。我在任何地方都找不到任何全面的样本或例子。

im9ewurl

im9ewurl1#

在csproj中添加以下Nuget包:

<PackageReference Include="Azure.Identity" Version="1.10.3" />
    <PackageReference Include="Azure.ResourceManager" Version="1.7.0" />
    <PackageReference Include="Azure.ResourceManager.AppService" Version="1.0.2" />

然后你可以使用下面的代码

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

var region = "eastus";
string resourceGroupName = "myResourceGroup";
string appServicePlanName = "myAppServicePlan";
string certificateName = "myAppCertificate";
string certPath = @"path_to_certificate.pfx";
string certPassword = "your_cert_password";

// Read PFX
byte[] pfxByteArray;
using (var stream = new FileStream(certPath, FileMode.Open, FileAccess.Read))
{
    pfxByteArray = new byte[stream.Length];
    stream.Read(pfxByteArray, 0, (int)stream.Length);
}

ArmClient client = new ArmClient(new DefaultAzureCredential());

SubscriptionResource subscription = client.GetSubscriptionResource(new Azure.Core.ResourceIdentifier("/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"));
ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();
ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(resourceGroupName);
var appServicePlan = await resourceGroup.GetAppServicePlanAsync(appServicePlanName);
var appCertificatesCollection = resourceGroup.GetAppCertificates();
AppCertificateData appCertificate = new AppCertificateData(region)
{
    Password = certPassword,
    PfxBlob = pfxByteArray,
    ServerFarmId = appServicePlan.Value.Id
};

appCertificatesCollection.CreateOrUpdate(WaitUntil.Completed, certificateName, appCertificate);

Console.ReadLine();

欲了解更多信息,请参阅
https://github.com/Azure/azure-sdk-for-net/blob/Azure.ResourceManager.AppService_1.0.2/doc/dev/mgmt_quickstart.md

https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.appservice?view=azure-dotnet
希望这对你有帮助。请为您的环境替换变量。

相关问题