不使用自定义域名时加载 Azure 证书

Loading Azure certificate when NOT using custom domain names

据我了解,如果有人不想使用自定义域名而是计划使用 Azure 分配给网站的 *.azurewebsite.net 域,那么 HTTPS 已经通过 Microsoft 的证书启用(我知道这不如使用自定义域名安全)。我将如何以编程方式加载此证书。目前我使用以下方法从本地计算机或 Azure 加载证书:

public static X509Certificate2 LoadFromStore(string certificateThumbprint,bool hostedOnAzure)
{
    var s = certificateThumbprint;

    var thumbprint = Regex.Replace(s, @"[^\da-zA-z]", string.Empty).ToUpper();

    var store = hostedOnAzure ? new X509Store(StoreName.My, StoreLocation.CurrentUser) : new X509Store(StoreName.Root, StoreLocation.LocalMachine); 

    try
    {
        store.Open(OpenFlags.ReadOnly);

        var certCollection = store.Certificates;

        var signingCert = certCollection.Find(X509FindType.FindByThumbprint, thumbprint, false);

        if (signingCert.Count == 0)
        {
            throw new FileNotFoundException(string.Format("Cert with thumbprint: '{0}' not found in certificate store. Also number of certificates in the sotre was {1}", thumbprint, store.Certificates.Count));
        }

        return signingCert[0];
    }
    finally
    {
        store.Close();
    }
}

我认为罪魁祸首是以下代码行:

new X509Store(StoreName.My, StoreLocation.CurrentUser) 

因为当我收到异常时它告诉我商店中没有证书,尽管我通过了正确的证书指纹(我从 Chrome 手动获取指纹)。

您将无法在您的 WebApp 中以编程方式访问此证书,因为此证书并未真正安装在 Azure WebApp 上。 Azure WebApps 有一个前端服务器执行 "kind of" SSL 卸载,因此 WebApp 实际上永远无法访问此特定证书。但是,您究竟为什么要阅读此证书?

通常情况下,如果 WebApps 中需要证书,您将安装 客户端证书 并将它们传递给服务以进行身份​​验证,如 https://azure.microsoft.com/en-us/blog/using-certificates-in-azure-websites-applications/ 中所述,以及您需要的那些证书可以编程方式访问(同一篇文章中提到的代码片段)

但是我不确定你到底想通过阅读服务器证书实现什么