如何从代码中访问Azure证书?

时间:2014-04-18 23:08:17

标签: .net azure certificate x509certificate2

我已将证书上传到azure,其配置如...

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="MyService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
  <Role name="MyRole">
    <Instances count="1" />
    <ConfigurationSettings>
          ...
    </ConfigurationSettings>
    <Certificates>
      <Certificate name="MyCert" thumbprint="CB3611F7D2406AB12094AE9489C50FE1A8B6BFF6" thumbprintAlgorithm="sha1" />
    </Certificates>
  </Role>
</ServiceConfiguration>

有什么类似......

X509Certificate2 myCert = Config.Certs["MyCert"];

甚至类似于将指纹直接拉出商店?我没有在运行时SDK中的任何地方看到我可以获得证书甚至是指纹。

2 个答案:

答案 0 :(得分:1)

无法枚举证书。实现此目标的标准方法是在csdef / cscfg中将cert指纹添加为配置设置,然后通过该配置设置在运行时查找指纹。

答案 1 :(得分:0)

private static X509Certificate2 GetCertificate()
    {
        var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection matchedCertificates =
                store.Certificates.Find(X509FindType.FindByThumbprint, "XXXXX", false);

        X509Certificate2 cert;
        if (matchedCertificates.Count > 0)
        {
           cert = matchedCertificates[0];
        }
        store.Close();            
        return cert;
    }