c# code access unauthorized

Hello,

The JS code below allows me to create a new case:

image

But when I want to translate it into my C# application, I probably miss one step because I receive un unauthorized acces:

var environmentContent = "application/json";
            var currentDate = DateTimeOffset.UtcNow;
            var content = new StringContent(serializedElement);
            var contentLength = Uri.EscapeUriString(serializedElement).Length;

            var dataToSign = "(request-target): post /v2/cases\n" +
                                "host: rms-world-check-one-api-pilot.thomsonreuters.com\n" +
                                "date: " + currentDate + "\n" +
                                "content-type: " + environmentContent + "\n" +
                                "content-length: " + contentLength + "\n" +
                                content;

            var provider = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(this.WorldCheckConfiguration.ApiSecret));
            var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
            var hmac = Convert.ToBase64String(hash);
            var authorisation = "Signature keyId=\"" + this.WorldCheckConfiguration.ApiKey + 
                            "\",algorithm=\"hmac-sha256\",headers=\"(request-target) host date content-type content-length\",signature=\"" + 
                            hmac + "\"";

            this.HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(environmentContent));
            this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", authorisation);
            this.HttpClient.DefaultRequestHeaders.Date = currentDate;          

            var url = this.WorldCheckConfiguration.ApiBaseUrl + "cases";
            var response = await this.HttpClient.PostAsync(url, content);

Best Answer

Answers