ASP.NET Web API Basic Authentication Authorisation Header
I noticed myself that if the Authorization-header only contained the key/token, the request.Headers.Authorization
wouldn't be initiated properly because it's looking for a scheme as well in the format <Scheme> <key/token>
, i.e. Authorization: Token VXNlcjpQYXNzd29yZA==
, then the Authorization
wouldn't be null anymore and contain request.Headers.Authorization.Scheme = "Token"
and request.Headers.Authorization.Parameter = "VXNlcjpQYXNzd29yZA=="
I've posted my own example of a Basic Authentication Attribute. Maybe this gives you some hints.
I use:
HttpContext.Current.Request.Headers["Authorization"];
And here is the link to the complete solution:
http://remy.supertext.ch/2012/04/basic-http-authorization-for-web-api-in-mvc-4-beta/
If you get stuck on this, you can get the header using:
var header = request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
But not via
var header = request.Headers.Authorization;
Though, this thread is very old but it might help others if I share how did I resolve it in my case:
Request should contain
Authorization: Basic VXNlcjpQYXNzd29yZA==
instead of:
Authorization: VXNlcjpQYXNzd29yZA==
so following change in request may solve the problem:
client.Headers.Add("Authorization", "Basic VXNlcjpQYXNzd29yZA==");