呼叫HTTPS WCF服务与认证证书 [英] Call a HTTPS WCF Service with Certificate authentication

查看:122
本文介绍了呼叫HTTPS WCF服务与认证证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF服务,并托管其在Windows Azure上。 WCF服务是一个HTTPS之一。当过我所说的服务的客户端需要一个证书来验证其真伪。

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.

当我在broswer键入服务URL时,要求提供验证证书和中的service运行。

When i type the service url on broswer it asks for a verifying certificate and the serivce runs.

到目前为止好。

现在我需要访问在MVC 4应用同样的服务。所以我做了一个简单的Ajax调用。

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>

但我不明白的结果。相反,我总是得到中止错误403。

But i dont get the result. Instead i always get abort error 403.


我是否需要写在web.config文件中的东西的MVC应用程序?我卡住,真正需要帮助在这里。

Do i need to write something on the web.config file in the MVC application? I am stuck and really need help out here.

感谢

推荐答案

得到了一个解决方案:

在Ajax调用我打了一个电话给控制器

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

        });
    });
});

然后在控制器:

[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类:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆