簡體   English   中英

調用具有證書身份驗證的HTTPS WCF服務

[英]Call a HTTPS WCF Service with Certificate authentication

我創建了一個wcf服務,並將其托管在Windows Azure上。 wcf服務是一個https。 每當我致電該服務時,客戶端都需要證書來驗證其真實性。

當我在瀏覽器上鍵入服務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文件上寫一些東西嗎? 我被困住了,真的需要這里的幫助。

謝謝

有一個解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM