Calling an Epicor Function via C# in an external program

We must have done something dumb. I’ll look at it after lunch.

Could there be a setting in Debug options that would prevent stepping on each line in the expression?

RCExample.zip (491.1 KB)

Here is my entire project, compiled as well.

Go to the bin\Debug\net6.0 folder and run:

RCExample.exe https://icanhazip.com

If that works, put your url instead.

Let me know.

Sample output:

SHA512: 8E7F0961D277F9BC0625FB4CA3F67420B97A257A2F25417BDDF15DC900D176F43FA3C5B8AE9F75FAA1E229DE20CED5F18A81B376DA9A91D2352F42C04FC73956
SHA256: FCF38396EF67FA4F5D83891F242291B83988DCABEE1FAD1B3917D859AA5EAC57
SHA1: 93F31408757802B63309BCCD53FA01C2474BF1F0
MD5: 7A05F41A04D70BCF20E916BC12EEFF07

I extracted the zip file and ran the .exe - No output

for icanhazip.com ?

Also made sure the files were all unblocked

For any URL…

Then you have something else going on either with your pc or your network.

What that would be, I cannot fathom.

OK… Must be some firewall issue. Our network is EXTREMELY locked down. I’ll try an internal address…

Some type of proxy you may have to go through perhaps?

I think that’s the issue with outside addresses… Just got the program to work using the
https://usaz1app008p.am.dir.grpleg.com/EpicorERPPilot/apps/erp/home/ and https://usaz1app008p.am.dir.grpleg.com/EpicorERPPilot/api/v2/efx/KEN/PostAPInvoice/PostAPGroup/ URLS! And the SHA512 hash looks NOTHING like the one I had. The SHA256 one however matches what I already had

Still doesn’t explain why the code in my program won’t perform the same way… I should get the same displays you are, but no dice…

when we were testing earlier, sometimes you were sending auth when not needed, and sending a post to a get, and having wrong accept for some pages, and comparing wrong hash etc

Try to go back to your post code, with auth, on the full endpoint, with the correct hash

According to REST help, I need to sent a POST request to call the function. Is that not true?

Yes, but when we were testing, we changed so much stuff around at once, we made a few mismatches I didn’t catch.

Go try your code like it was first thing this morning with SHA512 and the correct hash to match against

remember to make sure this is set to your function endpoint Properties.Settings.Default.AppServerURL

Send a POST request or GET request?

go back to this code

private static void CallPostFunction()
        {
            try
            {
                string remoteCertificateHash ="Correct SHA512 HashHere";

                HttpBasicAuthenticator httpBasicAuthenticator = new HttpBasicAuthenticator(Properties.Settings.Default.BatchEntryPerson, Properties.Settings.Default.BatchEntryPass);
                var options = new RestClientOptions(Properties.Settings.Default.AppServerURL)
                {
                    Authenticator = httpBasicAuthenticator
                    ,
                    RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
                    {
                        bool success = false;
                        string certHash = "";
                        certHash = certificate.GetCertHashString(System.Security.Cryptography.HashAlgorithmName.SHA512);
                        if (certHash == remoteCertificateHash)
                        {
                            success = true;
                        }
                        return success;
                    }
                };
                //System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, ce, ca, p) => true;
                var client = new RestClient(options);
                var request = new RestRequest();
                request.AddHeader("Accept", "application/json");
                request.AddHeader("X-API-Key", Properties.Settings.Default.APIKey);
                request.Method = Method.Post;
                var requestContentObj = new
                {
                    GroupID = $@"{BatchGroupID}"
                };
                string requestContent = JsonConvert.SerializeObject(requestContentObj);
                request.AddParameter("application/json", requestContent, ParameterType.RequestBody);
                var response = client.Execute(request);
                Console.WriteLine(response.ToString());
            }
            catch (Exception e)
            {
                throw new Exception("REST Call", e);
            }
        }

Changed the URL to the function endpoint, hash string to compare, put back the autthentication and the POST call… Here’s the code now:

        private static void CallPostFunction()
        {
            try
            {
                //string remoteCertificateHash = "3541208100360DA080792701B6378B578AB6FB102562C54877F51BAF7CDA709A";
                //string remoteCertificateHash = "F1E33102A19D2C8EDF43D9115F61574201686D1C177CC99AD30BEB7CB9C5F996";
                string remoteCertificateHash = "B5067BA1C676D4FCE860503FE8F24CC44E020E7F0564B1B985A523E60329CADB49F4429DAA6E3ECF0FDCFF33EC3ABF58076C54969162E32307585662C9FA7E56";

                HttpBasicAuthenticator httpBasicAuthenticator = new HttpBasicAuthenticator(Properties.Settings.Default.BatchEntryPerson, Properties.Settings.Default.BatchEntryPass);
                var options = new RestClientOptions(Properties.Settings.Default.AppServerURL)
                {
                    Authenticator = httpBasicAuthenticator,
                    RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
                    {
                        bool success = false;
                        string certHash = "";
                        certHash = certificate.GetCertHashString(System.Security.Cryptography.HashAlgorithmName.SHA512);
                        Console.WriteLine($"SHA512: {certHash}");

                        Console.WriteLine(remoteCertificateHash);
                        if (certHash == remoteCertificateHash)
                        {
                            success = true;
                        }
                        return success;
                        //return true;
                    }
                };
                //System.Net.ServicePointManager.ServerCertificateValidationCallback = (s, ce, ca, p) => true;
                var client = new RestClient(options);
                var request = new RestRequest();
                //request.AddHeader("Accept", "*/*");
                request.AddHeader("Accept", "application/json");
                request.AddHeader("X-API-Key", Properties.Settings.Default.APIKey);
                request.Method = Method.Post;
                //request.Method = Method.Get;
                var requestContentObj = new
                {
                    GroupID = $@"{BatchGroupID}"
                };
                string requestContent = JsonConvert.SerializeObject(requestContentObj);
                request.AddParameter("application/json", requestContent, ParameterType.RequestBody);
                var response = client.Execute(request);
                Console.WriteLine(response.ToString());
            }
            catch (Exception e)
            {
                throw new Exception("REST Call", e);
            }
        }

Running it now…