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

时间:2016-06-02 12:36:33

标签: c# azure ssl ssl-certificate azure-web-sites

据我了解,如果有人不想使用自定义域名,而是计划使用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) 

因为当我收到异常时它告诉我商店里没有证书,虽然我传递了正确的证书Thumbprint(我手动从Chrome中抓取指纹)。

1 个答案:

答案 0 :(得分:5)

您将无法在WebApp中以编程方式访问此证书,因为Azure WebApp上并未真正安装此证书。 Azure WebApps有一个前端服务器,它执行“一种”SSL卸载,因此WebApp实际上永远无法访问此特定证书。你为什么要阅读这个证书?

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

但我不确定您想要通过阅读服务器证书

来实现什么目标
相关问题