Calling an Epicor Function via C# in an external program

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…

I haven’t caught up to the whole thread but may I suggest you give a look at the EpicorRest NUGET we’ve written which handles all Epicor endpoints including certificate validation and is available for
.NET 4.8
.NET 6
and .NET Standard

It is fully documented and well understood and used in hundreds of projects. It may make your life much easier.

1 Like

Look at @josecgomez pluggin’ his warez, so proud :rofl:

Proud The Karate Kid GIF

But yes, he makes good stuff :slight_smile:

Thanks for chiming in, @josecgomez ! I have my target framework as .NET 7.0, is that considered .NET Standard?

Hey I’m just giving myself more work by having to support it #FreeAsInBeer

1 Like

No that’s the .NET regular version if you change your target to 4.8 you can use it otherwise there are older version available in nuget

Like I said I didn’t read the whole thread so if you are already wel on your way then you do you. But if you are still struggling this may softend the load though I would recommend upgrading to the latest version .NET 4.8

At this point, we’re so deep in, me MUST know!

I agree, I think I went to .NET 7.0 for something else I’m using in the project. I’d rather not rip everything up at this point. BTW, is there some guide as to what the version history is for .NET?

Ok I misunderstood NET 7 as NET 4.7… My brain insn’t working

You can use this one

Targets .NET 6 and would work in NET 7

schroeder dancing GIF
IT WORKS!!! :rofl: :smiling_face_with_tear: :heartbeat: :100:
You guys ARE THE BEST!!!

Which one?

wait which one works? the library? or the other stuff you were half 18/23rds way through troubleshooting lol

I was hoping we were 99%.

Bored Cabin Fever GIF

Everyone else:

Happy Hour Drinking GIF by Saturday Night LiveSeth Meyers Lol GIF by Late Night with Seth Meyers

Kevin’s solution worked! Apparently all I needed was the correct hash value, which was SHA512… @klincecum, THANK YOU for sticking with me and helping me with this project! I COULD NOT HAVE DONE IT without you!

@josecgomez, you’re ALWAYS as TERRIFIC help to me whether addressing a direct issue with me or just your brilliant answers to other questions. I owe BOTH of you a CASE of YOUR FAVORITE BEER!

If either of you are close to the SE Wisconsin area, I would LOVE to meet you guys! How can I buy you that beer?

1 Like