4

Windows azure サブスクリプションのすべての管理証明書を一覧表示しようとしています。そして、次のコードで試しました。しかし、それは私に例外を与えます。そして、それresponseがnullであり、例外メッセージが"The remote server returned an error: (403) Forbidden."

これで私を助けてください。msdnはこれの例を提供していません:(

using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
using System.Xml.Linq;

class ManagemenCertificateViewer
{
    public static void Runme()
    {
        string msVersion = "2012-03-01";
        string subscriptionId = "I used the subscription Id here";
        try
        {
            ListManagementCertificates(subscriptionId, msVersion);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught: ");
            Console.WriteLine(ex.Message);
        }
    }

    private static void ListManagementCertificates(string subscriptionId, string version)
    {
        string uriFormat = "https://management.core.windows.net/{0}/certificates";
        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.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 storageServices = responseBody.Element(wa + "SubscriptionCertificates");
            int mngmntCertificateCount = 0;
            foreach (XElement storageService in storageServices.Elements(wa + "SubscriptionCertificate"))
            {
                string publicKey = storageService.Element(wa + "SubscriptionCertificatePublicKey").Value;
                string thumbprint = storageService.Element(wa + "SubscriptionCertificateThumbprint").Value;
                string certificateData = storageService.Element(wa + "SubscriptionCertificateData").Value;
                string timeCreated = storageService.Element(wa + "TimeCreated").Value;
                Console.WriteLine(
                    "Certificate[{0}]{1}  SubscriptionCertificatePublicKey: {2}{1}  SubscriptionCertificateThumbprint: {3}{1} certificateData{4}{1} timeCreated{5}{1}",
                    mngmntCertificateCount++, Environment.NewLine, publicKey, thumbprint, certificateData, timeCreated);
            }
        }
        else
        {
            Console.WriteLine("List Management certificates returned an error:");
            Console.WriteLine("Status Code: {0} ({1}):{2}{3}",
                (int)statusCode, statusCode, Environment.NewLine,
                responseBody.ToString(SaveOptions.OmitDuplicateNamespaces));
        }
        return;
    }
}
4

2 に答える 2

3

おかげで思った通りに動いています。次の行とメソッド「GetCertificate(arg1)」を追加するだけです

request.ClientCertificates.Add(GetCertificate(certThumbprint));

もう 1 つ、Msdnヘルプ ガイドには、respond body というタグがあります。

<TimeCreated>time-created</TimeCreated>

しかし、API は作成したばかりの TimeCreated ではなく応答します。

<Created> ..... </Created>
于 2012-10-10T07:33:37.583 に答える
1

403 エラーは、Service Management API リクエストの認証に使用される管理証明書に問題があることを意味します。コード内のリクエストとともに管理証明書を添付しているとは思いません。このリンクは、サービス管理 API 要求の認証に役立つ場合があります: http://msdn.microsoft.com/en-us/library/windowsazure/ee460782

HTH。

于 2012-10-10T05:34:34.687 に答える