1

我创建了一个 wcf 服务并将其托管在 Windows azure 上。wcf 服务是一个 https 服务。每当我调用该服务时,客户端都需要一个证书来验证其真实性。

当我在 broswer 上键入服务 url 时,它会要求提供验证证书并运行服务。

在此处输入图像描述

到现在为止还挺好。

现在我需要在 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 文件上写一些东西吗?我被困住了,真的需要帮助。

谢谢

4

1 回答 1

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;
    }
}
于 2013-07-08T07:49:22.567 回答