2013-06-27 45 views
1

我問這個問題,因爲當我使用Azure REST API時,它需要客戶端證書。 在C#中,我可以寫這樣的代碼:如何使用Javascript/JQuery使X509證書發出REST請求?

**X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
     certificateStore.Open(OpenFlags.ReadOnly); 
     X509Certificate2Collection certs = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, 
      certThumbprint, false);** 

     if (certs.Count == 0) 
     { 
      Console.WriteLine("Couldn't find the certificate with thumbprint:" + certThumbprint); 
      return; 
     } 

     certificateStore.Close(); 

     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
      new Uri("https://management.core.windows.net/" + subscriptionId + "/services/hostedservices")); 

     request.Method = "POST"; 
     **request.ClientCertificates.Add(certs[0]);** 

但是我應該如何給JS樣的要求?

+1

一個選擇是讓你的js在你的服務器上打一個使用C#的請求的api。 –

+0

只有這樣? – EthenHY

+0

它適用於Windows 8應用程序或常規Web應用程序嗎? –

回答

2

不能。您不能使用純粹的(基於瀏覽器的)JavaScript/jQuery來針對Azure REST管理API進行編程。這有幾個原因。首先,Azure REST管理不支持CORS(跨源資源共享)。其他的一種是認證 - 在基於瀏覽器的JavaScript中無法使用X.509。