向所有传出改装请求添加动态 header

Add a dynamic header to all outgoing Refit requests

我在使用 IHttpClientFactory 的 .NET Core 3.1 应用程序中使用 Refit (5.1.67) 作为我的 HttpClient 包装器。

我正在呼叫的 API 使用客户端凭据令牌进行保护。

我正在用这个注册客户端:

services.AddRefitClient<ISomeApiClient>().ConfigureHttpClient(c =>
            c.BaseAddress = new Uri(Configuration["BaseUrlFromConfig"]));

客户端有如下所示的方法:

public interface ISomeApiClient
{
    [Get("/api/somewhere")]
    Task<IEnumerable<MyResponseObject>> GetItems([Header("X-User-Id")] string userId, [Header("Authorization")] string accessToken);

    [Get("/api/somewhere-else")]
    Task<MyResponseObject> GetItem([Header("X-User-Id")] string userId, [Header("Authorization")] string accessToken, int id);
}

我想避免的是每次调用端点时都必须显式传递 accessToken 和 userId(如上)。理想情况下,我想让我的客户看起来像这样:

public interface ISomeApiClient
{
    [Get("/api/somewhere")]
    Task<IEnumerable<MyResponseObject>> GetItems();

    [Get("/api/somewhere")]
    Task<IEnumerable<MyResponseObject>> GetItems(int id);
}

感觉我需要某种请求中间件来处理传出请求,我可以在其中添加这两个 headers。如果它们是静态的,我会装饰整个界面,但因为这些是运行时值,将不起作用。

我在文档中找不到任何关于这方面的帮助,如果有任何指点,我将不胜感激。

您可以使用 DI 在您需要的地方注入您的客户端。我们这样使用它:

[ApiController]
public class ValuesController : ControllerBase
{
private readonly ISomeApiClient_client;

public ValuesController(ISomeApiClient client)
{
   _client = client;
   httpClient.DefaultRequestHeaders.Authorization =
                     new AuthenticationHeaderValue("Bearer", "Your Oauth token");
}

[HttpGet("/")]
public async Task<ActionResult<Reply>> Index()
{
  return await _client.GetMessageAsync();
}
}

Refit 文档现在解释了如何执行此操作

https://github.com/reactiveui/refit#reducing-header-boilerplate-with-delegatinghandlers-authorization-headers-worked-example

添加一个 header 处理程序:

class AuthHeaderHandler : DelegatingHandler
 {
    private readonly IAuthTokenStore authTokenStore;

    public AuthHeaderHandler(IAuthTokenStore authTokenStore)
    {
         this.authTokenStore = authTokenStore;
    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var token = await authTokenStore.GetToken();

        //potentially refresh token here if it has expired etc.

        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

        return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
    }
}

然后在注册客户端时在Startup.cs注册:

services.AddTransient<AuthHeaderHandler>();

services.AddRefitClient<ISomeThirdPartyApi>()
        .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"))
        .AddHttpMessageHandler<AuthHeaderHandler>();