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;
}
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
2 Posts
Hmac authorization Unauthorized response for asp.net PostAsync. Working fine with .Net Console an...
Jan 26, 2021 07:37 AM|jjmbph|LINK
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>
</div>
Participant
960 Points
315 Posts
Re: Hmac authorization Unauthorized response for asp.net PostAsync. Working fine with .Net Consol...
Jan 27, 2021 08:59 AM|Jerry Cai|LINK
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
None
0 Points
2 Posts
Re: Hmac authorization Unauthorized response for asp.net PostAsync. Working fine with .Net Consol...
Feb 06, 2021 03:25 PM|jjmbph|LINK
Hello Jerry,
I managed to call the api thru hmac using a RestSharp. This can be closed.
Thanks!