如何使用REST Api在Windows Azure中列出管理证书(How to list Management Certificate In windows azure using REST Api)

我想在windows azure子目录中列出所有管理证书。 我尝试使用以下代码。 但它给了我一个例外。 我可以发现response为空,异常消息是"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; } }

I am tring to list all the management certificates in a windows azure subcription. And I tried with the following code. But it gives me an exception. And I could find that response is null and the exception message is "The remote server returned an error: (403) Forbidden."

Please help me with this. Msdn doesn't provide an example for this :(

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

最满意答案

403错误表示用于验证Service Management API请求的管理证书有问题。 我没有看到您在代码中附上管理证书以及您的请求。 您可能会发现此链接对于验证服务管理API请求非常有用: http : //msdn.microsoft.com/en-us/library/windowsazure/ee460782 。

HTH。

403 error means something wrong with your management certificate used to authenticate your Service Management API requests. I don't see you attaching a management certificate along with your request in your code. You may find this link useful for authenticating service management API requests: http://msdn.microsoft.com/en-us/library/windowsazure/ee460782.

HTH.

更多推荐

response,string,using,wa,电脑培训,计算机培训,IT培训"/> <meta name="desc