2012-10-03 18 views
1

我想列出使用Azure服務管理REST API的所有託管服務。 msdn hlep解釋了列出託管服務的一種方法。我附上了msdn中給出的示例代碼。應使用哪種拇指列印來列出使用Windows azure管理API的託管服務

在代碼中,他們使用了Version,Thumbprint和SubscriptionId。

在windows azure門戶中,我們可以看到一個subcription有一個subcription Id。並且證書有一個指紋。在一個subcription中可能有許多託管服務,所以許多證書也是如此。那麼以下代碼提到的指紋是什麼? 是否應該使用所有訂閱的指紋進行檢查,以列出所有託管服務的訂閱內容。

爲什麼我們不能使用subcriptionId來獲得所有的託管服務(是不是安全的?)還是有一個通用的證書(所以有一個指紋)的一個subcription?

請指導我,

謝謝。

namespace Microsoft.WindowsAzure.ServiceManagementRESTAPI.Samples 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Net; 
    using System.Security.Cryptography.X509Certificates; 
    using System.Xml; 
    using System.Xml.Linq; 

    public class Program 
    { 
     // Set these constants with your values to run the sample. 
     private const string Version = "2011-10-01"; 
     private const string Thumbprint = "management-certificate-thumbprint"; 
     private const string SubscriptionId = "subscription-id"; 

     static void Main(string[] args) 
     { 
      try 
      { 
       // Obtain the certificate with the specified thumbprint 
       X509Certificate2 certificate = GetStoreCertificate(Thumbprint); 
       ListHostedServicesExample(SubscriptionId, certificate, Version); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Exception caught in Main:"); 
       Console.WriteLine(ex.Message); 
      } 

      Console.Write("Press any key to continue:"); 
      Console.ReadKey(); 
     } 

     public static void ListHostedServicesExample(
      string subscriptionId, 
      X509Certificate2 certificate, 
      string version) 
     { 
      string uriFormat = "https://management.core.windows.net/{0}/" + 
       "services/hostedservices"; 
      Uri uri = new Uri(String.Format(uriFormat, subscriptionId)); 

      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
      request.Method = "GET"; 
      request.Headers.Add("x-ms-version", version); 
      request.ClientCertificates.Add(certificate); 
      request.ContentType = "application/xml"; 

      XDocument responseBody = null; 
      HttpStatusCode statusCode; 
      HttpWebResponse response; 
      try 
      { 
       response = (HttpWebResponse)request.GetResponse(); 
      } 
      catch (WebException ex) 
      { 
       // GetResponse throws a WebException for 400 and 500 status codes 
       response = (HttpWebResponse)ex.Response; 
      } 
      statusCode = response.StatusCode; 
      if (response.ContentLength > 0) 
      { 
       using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) 
       { 
        responseBody = XDocument.Load(reader); 
       } 
      } 
      response.Close(); 
      if (statusCode.Equals(HttpStatusCode.OK)) 
      { 
       XNamespace wa = "http://schemas.microsoft.com/windowsazure"; 
       XElement hostedServices = responseBody.Element(wa + "HostedServices"); 
       Console.WriteLine(
        "Hosted Services for Subscription ID {0}:{1}{2}", 
        subscriptionId, 
        Environment.NewLine, 
        hostedServices.ToString(SaveOptions.OmitDuplicateNamespaces)); 
      } 
      else 
      { 
       Console.WriteLine("Call to List Hosted Services returned an error:"); 
       Console.WriteLine("Status Code: {0} ({1}):{2}{3}", 
        (int)statusCode, statusCode, Environment.NewLine, 
        responseBody.ToString(SaveOptions.OmitDuplicateNamespaces)); 
      } 
      return; 
     } 

     /// <summary> 
     /// Gets the certificate matching the thumbprint from the local store. 
     /// Throws an ArgumentException if a matching certificate is not found. 
     /// </summary> 
     /// <param name="thumbprint">The thumbprint of the certificate to find.</param> 
     /// <returns>The certificate with the specified thumbprint.</returns> 
     private static X509Certificate2 GetStoreCertificate(string thumbprint) 
     { 
      List<StoreLocation> locations = new List<StoreLocation> 
      { 
       StoreLocation.CurrentUser, 
       StoreLocation.LocalMachine 
      }; 

      foreach (var location in locations) 
      { 
       X509Store store = new X509Store("My", location); 
       try 
       { 
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); 
        X509Certificate2Collection certificates = store.Certificates.Find(
         X509FindType.FindByThumbprint, thumbprint, false); 
        if (certificates.Count == 1) 
        { 
         return certificates[0]; 
        } 
       } 
       finally 
       { 
        store.Close(); 
       } 
      } 

      throw new ArgumentException(string.Format(
       "A Certificate with Thumbprint '{0}' could not be located.", 
       thumbprint)); 
     } 
    } 
} 

回答

3

,你想使用的證書是「管理證書」。以下是這樣做的過程:

  1. 在您的計算機上創建一個自簽證書(pfx文件格式)。您可能會發現此鏈接有用:http://consultingblogs.emc.com/gracemollison/archive/2010/02/19/creating-and-using-self-signed-certificates-for-use-with-azure-service-management-api.aspx
  2. 在您的本地證書存儲區中安裝證書(最好是CurrentUser \ My)。
  3. 以.cer文件格式從計算機上的本地證書存儲區導出證書。
  4. 在門戶網站的管理證書部分下載此證書。爲此,請登錄到Windows Azure門戶(https://manage.windowsazure.com),然後單擊「設置」選項卡,然後單擊「上傳」按鈕選擇並上傳此文件。

有幾件事情要記住:

  1. 您可以每股認購多達10個管理證書。
  2. 如果您希望您的同事使用相同的證書,請分享步驟1中創建的pfx文件,並讓他們將證書安裝在本地計算機的證書存儲區中。請不要給他們在步驟3中創建的.cer文件,因爲它沒有證書的私人數據。

希望這會有所幫助。

相關問題