简体   繁体   中英

Call a HTTPS WCF Service with Certificate authentication

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM