使用证书身份验证调用HTTPS WCF服务 [英] Call a HTTPS WCF Service with Certificate authentication
问题描述
我创建一个wcf服务并托管在windows azure。 wcf服务是一个https。当我调用服务时,客户端需要一个证书来验证其真实性。
当我在broswer上键入服务url时,它要求一个验证证书,并运行serivce。
>
到目前为止很好。
现在我需要在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。
我需要在MVC应用程序的web.config文件中写一些东西吗?我被困在这里真的需要帮助。
感谢
b
$ b
在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)
{
输出+ =新字符串(ReadBuf);
}
} while(count> 0);
}
catch(例外)
{
//抛出异常... lol:P
}
$ b b 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)
{
//你可以自己做证书检查。
//您可以从WinError.h获取错误值。
//返回true,以便任何证书将与此示例配合使用。
return true;
}
}
i create a wcf service and hosted it on windows azure. The wcf service is a https one. When ever i call the service the client needs a certificate to verify its authenticity.
When i type the service url on broswer it asks for a verifying certificate and the serivce runs.
So far so good.
Now i need to access the same service in an MVC 4 application. So i made a simple ajax call.
<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>
But i dont get the result. Instead i always get abort error 403.
Do i need to write something on the web.config file in the MVC application? I am stuck and really need help out here.
Thanks
Got a solution:
In the ajax call i made a call to the controller
<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);
}
});
});
});
Then in the controller:
[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);
}
The CertPolicy Class:
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;
}
}
这篇关于使用证书身份验证调用HTTPS WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!