Call a HTTPS WCF Service with Certificate authentication

前端 未结 1 899
南旧
南旧 2021-01-25 13:31

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.

相关标签:
1条回答
  • 2021-01-25 13:56

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题