I'm trying to upload a PFX SSL certificate to Azure App Service Web App using Azure SDK.NET. I have this question but it uses the deprecated Microsoft.Azure.Management.Fluent package. I couldn't find any comprehensive sample or example anywhere.
Add Following Nuget Packages in csproj:
<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" />
Then you can use code below
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();
For More information you can refer to
And
https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.appservice?view=azure-dotnet
Hope this helps. Please replace the variables for your environment.