Is it possible to populate request headers with custom headers in middleware? My aim is making calls to external rest api which has basic auth implemented. So it would be nice to keep API secrets in back-end code.
I have tried all methods that possibly could add header to request headers in HTTP context but seems it is a bit harder then I thought before.
There is no problems to modify response headers maybe there is such way with request headers?
There is a difference between modifying requests and responses here.
Since you're calling a rest api you need to inject an IHttpClientFactory
> Create a client > Add the auth headers to it via appsettings.json (for example)
or
You can use Typed Clients
which can be configured like this:
services.AddHttpClient<GithubClient>(c => { c.DefaultRequestHeaders.Add("Bearer", "token"); });
You can add your auth headers from the config above using the DefaultRequestHeaders
property then inject an HttpClient
in the your typed service which will be responsible for making the calls to your rest api.
Everything about this topic is available in the docs. See here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.