locked
Hmac authorization Unauthorized response for asp.net PostAsync. Working fine with .Net Console and Postman RRS feed

  • Question

  • User1119075522 posted

    Hi Guys,

    Just need your guidance here. I need to access an API with Hmac authentication. I was able to successfully send a request GET/POST using Postman and .net Console app. But when I use the same code with asp.net mvc, it gives an Unauthorized response from the API using a POSTASYNC. The same code working for GETASYNC request

    I am generating the hmac using a secret key and json payload. This json which I also passed as HTTPContent. Here is the snippet of my post request.

    <div>           

    string hmackey = CalculateHmac(key, PayLoad);
                HttpResponseMessage response = new HttpResponseMessage();
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(uri);
                    client.Timeout = new TimeSpan(100000000);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Add("X-User-ID", user);
                    client.DefaultRequestHeaders.Add("X-Group-ID", group);
                    client.DefaultRequestHeaders.Add("X-User-Token", token);
                    client.DefaultRequestHeaders.Add("X-Hmac", hmackey);
                    HttpContent content = new StringContent(PayLoad, Encoding.UTF8, "application/json");
                    response = await client.PostAsync(uri, content);
                    result = response.Content.ReadAsStringAsync().Result;
                }


    </div>

    Tuesday, January 26, 2021 7:36 AM

All replies

  • User1312693872 posted

    Hi,jjmbph

    Is this api from the third party? and no matter it's yours or the third party, you should follow the rules, make sure it needs that four headers

    and didn't miss the others.

    How do you get the token. make sure they are still within the valid time.

    And try to not use uri to send post , just use string instead to check whether the problems lies in the uri, it can work on postman but can't on

    console, so you should check the differences, have you changed the code?

    Best Regards,

    Jerry Cai

    Wednesday, January 27, 2021 8:59 AM
  • User1119075522 posted

    Hello Jerry,

    I managed to call the api thru hmac using a RestSharp. This can be closed.

    Thanks!

    Saturday, February 6, 2021 3:25 PM