Search code examples
c#asp.net-web-apidotnet-httpclient

Using HttpClient, send an Authorization token with no schema


Note: This question is similar to this one, but its suggested answer doesn't apply here.

I am trying to access this API, which is looking for a header that looks like this:

Authorization: {token}

Note the absence of any authentication scheme.

I have tried doing:

myHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("", authToken);

Which resulted in an ArgumentException saying that I can't pass an empty string.

I have also tried both:

myHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(authToken);

and

myHttpClient.DefaultRequestHeaders.Add("Authorization", authToken);

Both of which result in a FormatException.

There seems to be something forcing me to conform to a standard format for my authorization header, but the service I am trying to access doesn't use that standard format.


Solution

  • You could try:

    request.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token));
    

    as mentioned in https://learn.microsoft.com/en-us/dotnet/api/system.net.http.headers.httpheaders.tryaddwithoutvalidation.