1

Azure Service ManagementRESTApiを使用してホストされているすべてのサービスを一覧表示したいと思います。また、msdn hlepは、ホストされているサービスを一覧表示する方法を説明しています。msdnで提供されているサンプルコードを添付しました。

コードでは、Version、Thumbprint、SubscriptionIdを使用しています。

Windows Azureポータルでは、サブスクリプションにサブスクリプションIDがあることがわかります。また、証明書には拇印があります。1つのサブスクリプションに多数のホストされたサービスが存在する可能性があるため、証明書も多数存在します。それで、次のコードが言及している指紋は何ですか..?サブスクリプトのすべての拇印をチェックして、サブスクリプト内のすべてのホストされているサービスを一覧表示する必要があります。

subcriptionIdを使用するだけですべてのホストされたサービスを取得できないのはなぜですか(保護されていませんか?)または、サブスクリプションに共通の証明書(指紋があります)がありますか?

案内してください、

ありがとう。

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));
        }
    }
}
4

1 に答える 1

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ファイルには証明書のプライベートデータが含まれていないため、提供しないでください。

お役に立てれば。

于 2012-10-03T04:26:57.533 に答える