runtime: Setting Authorization header on an HttpClient instance does not work in .NET Core 2.1
@pereiraarun commented on Mon Jun 11 2018
Testing on .NET Core 2.1 (by setting Target Framework 2.1), the following code results in a 403 Forbidden since the header is not set correctly. Header is set using the following method:
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", "9ac..b87b20");
var result = client.GetStringAsync("/api/ipam/prefixes/").Result;
Moving back to .NET Core 2.0 (by setting Target Framework 2.0) fixes the issue. The same happens when using RestSharp. Seems there is some bug with HttpClient and setting authorization headers.
Testing through: Vs Professional 2017 (15.7.3) on Windows 10 with the latest updates.
@brockallen commented on Mon Jun 11 2018
Are you sure the scheme is correct? Normally it should be “Bearer” (not “Token”) if you’re doing an OAuth2 style client.
@pereiraarun commented on Tue Jun 12 2018
The code works as posted in .Net Core 2.0. The code is used for https://netbox.readthedocs.io/en/latest/api/authentication/
$ curl -H "Authorization: Token d2f763479f703d80de0ec15254237bc651f9cdc0" -H "Accept: application/json; indent=4" http://localhost/api/dcim/sites/
{
"count": 10,
"next": null,
"previous": null,
"results": [...]
}
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (7 by maintainers)
I had the same problem and found it was related to an automatic redirect. For a temporary fix, I was able to use the URL I was being redirected to instead. It seems like the authentication header is being lost during the redirect.
If you disable AllowAutoRedirect on the HTTP client, can you check if you’re being redirected?
FYI: 2 weeks ago we released a security fix to remove
Authorizationrequest headers from redirects. See dotnet/corefx#32730. .NET Core 2.0 didn’t get the patch because it is out of support as of 10/1.If anyone hits the problem without redirects being involved, please let us know. That is something we would look into. We would need repro or further details in such case to make progress.
@MelbourneDeveloper I believe Microsoft’s official solution for this at the moment of writing this comment (found on MSDN) is to write your own authentication module, which is not ideal. Our request to a url has a redirect that changes every year, sometimes more than once so it’s unreasonable to use
CredentialsCachefor our use case.In my opinion, an option should just be added to not remove headers on redirect.
Yes. That behavior is by-design. ‘Authorization’ request headers are removed during redirects.
@karelz , I understand why the security fix was added, but doesn’t this raise another important issue? What if there is some other sensitive header included in the original request. Won’t that get sent as part of the redirect?
Shouldn’t there be a callback on HttpClient or the HttpClientHandler that exposes the headers so that we can add or remove them as necessary?
Are we meant to write handler code on every http call that may redirect as @chrisipeters has demonstrated? That’s very onerous and only deals with the problem after the fact.
PS: This has probably been going on since the early versions of HttpClient / HttpClientHandler and probably has implications for all the different platforms. I think I’m experiencing headers being stripped because of redirects in .NET 4.5. What is Microsoft’s recommended approach to this, and are there long term plans to add a callback to that this problem can be dealt with in a graceful way?
Thanks all, the security change about removing Authorization headers is in fact what was going on in my case. For those still working through it, here’s the code I have - working now: Adapted from: https://stackoverflow.com/a/28671822/5043701
There are ways to preserve them though. That requires using a
CredentialsCacheobject and populating it with credentials assigned to specific Uri paths. Then, assign that object to the HttpClientHandler.Credentials property.However, manually adding ‘Authorization’ request headers is not a recommended pattern anyways. And those headers will be removed during redirects.
+1 this issue. Didn’t have it it 2.0 but now have it in 2.1.
It seems to work fine when PUTing/POSTing to another .NET Core application. When posting to a .NET Framework (4.6) project the following occurs:
Code:
Server side, I explicitly throw an exception and iterate through the headers. Notice authorization is not even there.
“errorResponse”:
Content-Length= 2239, Content-Type= application/json; charset=utf-8, Cookie= ASP.NET_SessionId=<sessionidstring>, Host= mydomain.com, Request-Context= appId=<appidstring>, Request-Id= <requestidstring>