AnsweredAssumed Answered

InsertPIData Client Certificate issue

Question asked by DSmith1 on Jun 29, 2015
Latest reply on Jul 28, 2015 by dng

I am writing a C# application that calls our PI Web Service. I have tried to generate code using both "Add a Service Reference (based off .NET 2.0 framework)" and "Add a Web Reference (based off .NET 4.0 framework)".

 

WIth both instances, I generate a PITimeSeries client and add the client certificate from my certificate store. I verified that the correct certificate is part of the client.ClientCertificates X509 certificate collection after adding it.

But when I call the InsertPIData method I get an error that says "Policy Falsified". A certificate is not being sent with the request to the designated endpoint.

 

Why is the InsertPIData method not sending the client certificate with it?

 

Here is a code snippet to show what I'm doing. First one is with the .NET 2.0 framework

 

PITimeSeriesService client = new PITimeSeriesService();

            // string to the destination PI tag
            const string PATH = @"pi:\\MYSERVER\PITAG";

            // String to host name of client certificate
            const string thumbprint = "correct thumbprint (excluded for confidentiality)";

            X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            certStore.Open(OpenFlags.ReadOnly);
            X509Certificate myCert = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, true)[0];
            certStore.Close();
            client.ClientCertificates.Add(myCert);

            // Set the timestamp & value for the PI tag
            // Here is where generators need logic to pull mw output from their system
            TimedValue mWoutput = new TimedValue();
            mWoutput.Time = DateTime.Now;
            mWoutput.Value = "38";     // mWoutput should come from provider

            // insertPIData method requires a TimeSeries array object
            TimeSeries series = new TimeSeries();
            series.Path = PATH;
            TimedValue[] values = { mWoutput };
            series.TimedValues = values;
            TimeSeries[] data = { series };   // here is where multiple PI points could be updated

            // use client variable to call InsertPIData method, replacing duplicate values
            try
            {
                client.InsertPIData(data, InsertPIDataDuplicateSwitch.InsertDuplicate);
            }
            catch (Exception e)
            {
                string error = "Exception occured when calling method: " + e;
                Console.Write(error);
            }

 

 

Here is with using .NET 4.0 framework

 

// string to the destination PI tag
            const string PATH = @"pi:\\MYSERVER\PITAG";

            // String to host name of client certificate
            const string thumbprint = "correct thumbprint (excluded for confidentiality)";

            X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            certStore.Open(OpenFlags.ReadOnly);
            X509Certificate2 myCert = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, true)[0];
            certStore.Close();

            // Declare web-service client and set certificate from Local Cert Store
            PITimeSeriesClient client = new PITimeSeriesClient();
            client.ClientCredentials.ClientCertificate.Certificate = myCert;
            
            // Set the timestamp & value for the PI tag
            // Here is where generators need logic to pull mw output from their system
            TimedValue mWoutput = new TimedValue();
            mWoutput.Time = DateTime.Now;
            mWoutput.Value = Convert.ToString(DateTime.Now.Second);     // mWoutput should come from provider

            // insertPIData method requires a TimeSeries array object
            TimeSeries series = new TimeSeries();
            series.Path = PATH;
            TimedValue[] values = { mWoutput };
            series.TimedValues = values;
            TimeSeries[] data = { series };             // here is where multiple PI points could be updated

            // use client variable to call InsertPIData method, replacing duplicate values
            try
            {
                client.InsertPIData(data, InsertPIDataDuplicateSwitch.InsertDuplicate);
            }
            catch (Exception e)
            {
                string error = "Exception occured when calling method: " + e;
                Console.Write(error);
            }

 

Do you see anything I'm doing wrong? Why is the certificate not being included with the request?

Outcomes