You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure SDK向Azure App Service Web应用上传PFX SSL证书

使用Azure SDK .NET上传PFX证书到Azure App Service Web应用

前置条件

  • 安装NuGet包:Azure.ResourceManager.AppService 和 Azure.Identity
  • 拥有Azure账号的Contributor或更高权限,能管理目标资源组、Web应用及证书
  • 准备好PFX证书文件和对应的密码

完整示例代码

using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.AppService;
using Azure.ResourceManager.AppService.Models;
using System.IO;

// 替换为你的Azure资源信息
var tenantId = "你的租户ID";
var clientId = "服务主体客户端ID";
var clientSecret = "服务主体密钥";
var subscriptionId = "订阅ID";
var resourceGroupName = "资源组名称";
var webAppName = "Web应用名称";
var pfxFilePath = @"本地PFX文件路径";
var pfxPassword = "PFX证书密码";
var certificateName = "自定义证书名称";

// 初始化ArmClient(服务主体身份验证)
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var armClient = new ArmClient(credential, subscriptionId);

// 获取目标资源组和Web应用
var resourceGroup = armClient.GetResourceGroupResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"));
var webApp = await resourceGroup.GetWebApps().GetAsync(webAppName);

// 读取PFX证书字节流
byte[] pfxBlob = File.ReadAllBytes(pfxFilePath);

// 构建证书创建参数
var certData = new AppServiceCertificateData
{
    PfxBlob = pfxBlob,
    Password = pfxPassword,
    FriendlyName = certificateName
};

// 上传证书到资源组(证书属于资源组级别,可绑定至组内任意Web应用)
var certCollection = resourceGroup.GetAppServiceCertificates();
var createOperation = await certCollection.CreateOrUpdateAsync(WaitUntil.Completed, certificateName, certData);
var uploadedCert = createOperation.Value;

// 可选:将证书绑定到Web应用的自定义域名
// var hostnameBindings = webApp.GetHostNameBindings();
// var targetHostname = await hostnameBindings.GetAsync("你的自定义域名");
// var sslBinding = new SslBindingData(SslState.SniEnabled, uploadedCert.Id);
// await targetHostname.GetSslBinding().CreateOrUpdateAsync(WaitUntil.Completed, "ssl-binding", sslBinding);

Console.WriteLine($"证书上传完成,证书ID:{uploadedCert.Id}");

重要说明

  • 示例使用Azure官方推荐的Azure.ResourceManager系列SDK,替代了已弃用的Microsoft.Azure.Management.Fluent包
  • 身份验证方式可根据场景调整:本地开发可改用DefaultAzureCredential(自动适配本地环境的Azure登录信息)
  • 证书创建在资源组级别,上传后可通过Azure Portal或代码绑定到多个Web应用

内容的提问来源于stack exchange,提问作者magdy.roshdy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 06:15:15