I'm trying to add an "X-Auth-Token" as a header on my HttpClient and I'm getting an 403 error forbidden when I make the request, which makes sense because I don't think my X-Auth-Token is being attached as a header.
How can I specify "X-Auth-Token" in my Header?
Here is the relevant code:
using (var c = new HttpClient())
{
c.BaseAddress = new Uri(url);
c.DefaultRequestHeaders.Accept.Clear();
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token);
The header X-Auth-Token is designed to authenticate request that doesn't contain secure cookie. e.g., API requests from notebook.
Basically, the Authorization: Basic is used to log in and then you return a generated token which is returned on further requests to prove who you are.
You can use the Add
method to add the header.
c.DefaultRequestHeaders.Add("x-auth-token", token);
The constructor for AuthenticationHeaderValue accepts a scheme
. I'm not certain what that is but is likely to be on of these
http://msdn.microsoft.com/en-us/library/ms789031(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With