I'm using a C# Refit client to have my services talk to one another via http.
I'm trying to send a Bearer token through the Authorization header, but according to the error message, it's not setting the AZ header in the request (see bottom). I've tried setting it through providing all headers, and using the [Authorize]
attribute, and all the other methods they describe in their readme.
Here is my Refit client api call definition:
[Post(PresentationsBasePath + "/{presentationId}/cart")]
Task AddItemToCartAsync(long presentationId, ShoppingCartItemView item, [HeaderCollection] IDictionary<string, string> headers);
//calling it here:
await _api.AddItemToCartAsync(presentationId, item, GetTokenHeader(presentationId, token));
private Dictionary<string, string> GetTokenHeader(long presentationId, string token) => new()
{
["pres_id"] = presentationId.ToString(),
[HeaderNames.Authorization] = $"Bearer {token}",
};
However, I'm getting a 401, and looking at the Refit.ApiException
that's thrown, the RequestMessage.Headers
does not contain the Authorization header.
Here's how I'm registering my refit api IPresentationsApi
. I'm not doing anything relating to auth in the DI configuration
var refitSettings = GetRefitSettings();
void Configure<T>() where T : class => services
.AddRefitClient<T>()
.ConfigureHttpClient(ConfigureHttpClient);
Configure<IMarsPresentationApi>();
//other apis configured below
private static void ConfigureHttpClient(IServiceProvider sp, HttpClient client)
{
var config = sp.GetRequiredService<IMarsConfiguration>();
if (config.BaseUrl == null)
throw new InvalidOperationException("Mars:BaseUrl must be configured");
client.BaseAddress = new Uri(config.BaseUrl);
}
Error shown here- you can see I get 401, and AZ header is not set in the request:
What am I doing wrong? How do I get it to send the AZ header?