2012-10-10 66 views
4

我打算列出Windows Azure中的所有管理證書。我試着用下面的代碼。但它給了我一個例外。我可以發現response爲空,異常消息爲"The remote server returned an error: (403) Forbidden."如何使用REST API列出Windows Azure中的管理證書

請幫我解決這個問題。 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; 
    } 
} 

回答

1

403錯誤意味着什麼毛病來驗證您的服務管理API請求你的管理證書。我沒有看到你與你的請求一起附加管理證書。在你的代碼你會發現這個鏈接有用的認證服務管理API請求:。http://msdn.microsoft.com/en-us/library/windowsazure/ee460782

HTH

+0

我在腦海中有這個問題:)我們需要認證我們的自我。這就是你說的對嗎?所以我們總是需要與我們保持一個單獨的管理證書來使用API​​。 我會嘗試的 感謝您的快速響應 –

+1

基本上它是一個catch 22的情況:)爲了管理管理證書,您至少需要一個管理證書。您可以做的一件事就是利用第一個證書的發佈設置文件,然後將其用於管理證書的進一步操作。 –

3

由於它的工作,我希望我只是添加以下行和方法「GetCertificate(ARG1) '

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

還有一兩件事,在Msdn幫助指導有一個在響應體標籤叫

<TimeCreated>time-created</TimeCreated> 

但是API不響應其剛剛創建的TimeCreated。

<Created> ..... </Created> 
相關問題