2014-09-30 98 views
2

我在我的應用程序中使用了azure服務管理REST API。我上傳了天藍色的管理證書,並在當地有一份副本。 我將認證保存在應用程序本身的單獨文件夾(AzureCertificate)中並引用該位置。 e.g:使用Azure REST API的管理證書

string certificatePath = Server.MapPath("~/AzureCertificate/") + certificateName;

X509Certificate2 certificate = new X509Certificate2(certificatePath);

AzureCertificate -- Folder name certificateName - MyCertificatieName.cer

它工作正常,當我運行該應用程序我的本地開發環境。但是當我在Azure網站上部署相同的錯誤時,我遇到了以下錯誤。

The remote server returned an error: (403) Forbidden

我這是怎麼發出請求

string uri = apiURL + subscriptionId + "/services/hostedservices";

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);

X509Certificate2 certificate = new X509Certificate2(certificatePath);

req.ClientCertificates.Add(certificate);

req.Headers.Add("x-ms-version", "2009-10-01"); HttpWebResponse res =

(HttpWebResponse)req.GetResponse();

但在最後一行拋出上面說的異常(req.GetResponse())。

我們可以用這種方式使用管理證書嗎?

我的要求是開發一個應用程序,它使用Azure REST API並在Azure中進行部署。

+0

您試圖訪問哪個apiurl? – 2014-09-30 17:03:27

回答

0

我建議使用Azure Management SDK。您可以安裝名爲Microsoft.WindowsAzure.Management的nuget軟件包,並使用適當的類/方法來執行您想要執行的操作。

如果你確實需要通過HTTP和REST API直接做些什麼,我建議使用HttpClient而不是HttpWebRequest。 (HttpClient是另一個NuGet包命名爲Microsoft.Net.Http然後可以使用SubscriptionCloudCredntials(通過ManagementClient.Credentials屬性)來填充你的HTTP請求,例如:。

var client = new ManagementClient(
    new CertificateCloudCredentials(subscriptionId, certificate)); 
//... 
var requestMessage = new HttpRequestMessage(HttpMethod.Get, apiURL); 
await client.Credentials.ProcessHttpRequestAsync(requestMessage, 
    CancellationToken.None); 
var httpClient = new HttpClient(); 
HttpResponseMessage response = await httpClient.SendAsync(requestMessage); 
// TODO: process response, maybe: 
var responseText = response.AsString(); 

我推薦使用client當你可以

+0

但是,它可能仍然需要證書嗎?我正在使用(https://management.core.windows.net//services/hostedservices) – 2014-10-02 11:28:08

+0

是的,這是證書參數 – 2014-10-02 12:14:05

1

我還發現,正是創建證書與管理API使用正確的方法是非常重要的 - 我得到的403錯誤,直到我用這個腳本創建證書:

makecert -r -pe -a sha1 -n "CN=Windows Azure Authentication Certificate" -ss my -len 2048 -sp "Microsoft Enhanced RSA and AES Cryptographic Provider" -sy 24 ManagementApiCert.cer 

我在那裏得到:http://blogs.msdn.com/b/davidhardin/archive/2013/08/27/azure-management-certificate-public-key-private-key.aspx這是一個幾歲,但爲我工作,當我嘗試其他新的沒有。

此外,請確保您在門戶網站的「設置」中的「管理證書」下上載證書,但不是SSL或遠程訪問證書。

+0

您保存了我的一天。謝謝 – jekcom 2016-02-02 14:58:55