Search code examples
c#.nethttp-headersauthorizationdotnet-httpclient

Why is the scheme required for AuthenticationHeaderValue?


I am setting the authorization header of an HttpClient in the following manner:

httpClient
    .DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(null, "abc");

...but am getting the exception:

"scheme" of the AuthenticationHeaderValue cannot be null.

Why must the AuthenticationHeaderValue have a scheme? Is this required by a specific RFC?


Solution

  • The scheme is used to determine what kind of authentication you are using:

    • Basic
    • Oauth
    • Bearer
    • Digest
    • etc.

    The header will look like this:

    {
       "key": "Authorization",
       "value": "<scheme> <parameter>"
    }
    

    Try to use Postman to see what is generated based on the different types of authentication supported by HTTP.