Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add X-Auth-Token - C# HttpClient

Tags:

c#

httpclient

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);
like image 742
Jeremy Wagner Avatar asked Dec 17 '14 18:12

Jeremy Wagner


People also ask

What is a X-auth-token?

The header X-Auth-Token is designed to authenticate request that doesn't contain secure cookie. e.g., API requests from notebook.

How does X-auth-token work?

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.


1 Answers

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

like image 173
Tejas Sharma Avatar answered Oct 12 '22 22:10

Tejas Sharma