2013-07-08 161 views
1

我創建了一個wcf服務並將其託管在Windows Azure上。 wcf服務是一個https服務。當我打電話給客戶時,客戶需要證書來驗證其真實性。使用證書認證來調用HTTPS WCF服務

當我在broswer上鍵入服務url時,它會要求驗證證書,並運行serivce。

enter image description here

到目前爲止好。

現在我需要訪問MVC 4應用程序中的相同服務。所以我做了一個簡單的ajax調用。

<script> 
$(document).ready(function() { 
    $("#GetAdjustedSalary").click(function() { 
     var salary = parseFloat($("#salary").val()); 
     var infalation = parseFloat($("#inflation").val()); 

     $.ajax({ 
      url: "https://newtonsheikh.cloudapp.net/SalaryService.svc/adjustedsalary?a=" + salary + "&b=" + infalation, 
      type: "GET", 
      dataType: "JSON", 
      contentType: "application/json", 
      success: function (data) { 
       alert(data); 
      } 

     }); 
    }); 
}); 
</script> 

但我沒有得到結果。相反,我總是得到中止錯誤403

enter image description here enter image description here

我是否需要寫在web.config文件中的東西MVC應用?我被卡住了,真的需要幫助。

由於

回答

1

得到的溶液:

在AJAX調用我在控制器進行的呼叫到所述控制器

<script> 
$(document).ready(function() { 
    $("#GetAdjustedSalary").click(function() { 
     var salary = parseFloat($("#salary").val()); 
     var infalation = parseFloat($("#inflation").val()); 

     var object = { 
      salary: salary, 
      infalation: infalation 
     } 

     var data = JSON.stringify(object); 

     $.ajax({ 
      url: "Home/GetData/", 
      type: "POST", 
      data: data, 
      dataType: "JSON", 
      contentType: "application/json", 
      success: function (data) { 
       $("#answer").html(data); 
      } 

     }); 
    }); 
}); 

然後:

[HttpPost] 
    public ActionResult GetData(string salary, string infalation) 
    { 
     string output = ""; 

     try 
     { 
      X509Certificate Cert = X509Certificate.CreateFromCertFile("d://Cert//newton2.cer"); 

      ServicePointManager.CertificatePolicy = new CertPolicy(); 
      HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://newtonsheikh.cloudapp.net/SalaryService.svc/adjustedsalary?a="+salary+" &b="+infalation+""); 
      Request.ClientCertificates.Add(Cert); 
      Request.UserAgent = "Client Cert Sample"; 
      Request.Method = "GET"; 
      HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); 
      Console.WriteLine("{0}" + Response.Headers); 
      Console.WriteLine(); 

      StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.Default); 
      int count; 

      char[] ReadBuf = new char[1024]; 
      do 
      { 
       count = sr.Read(ReadBuf, 0, 1024); 
       if (0 != count) 
       { 
        output += new string(ReadBuf); 
       } 

      } while (count > 0); 

     } 
     catch (Exception ex) 
     { 
      //Throw the exception...lol :P 
     } 

     output = output.Replace("\0", ""); 

     string jsonString = JsonConvert.SerializeObject(output, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 

     return Json(jsonString, JsonRequestBehavior.AllowGet); 
    } 

CertPolicy類別:

class CertPolicy : ICertificatePolicy 
{ 
    public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) 
    { 
     // You can do your own certificate checking. 
     // You can obtain the error values from WinError.h. 

     // Return true so that any certificate will work with this sample. 
     return true; 
    } 
}