I am to do the following using Abp's dynamic c# client proxies:
- Register two dynamic client proxies, e.g. one for "FoobarService" and one for "BazbarService" using
AddHttpClientProxies. The service "MyEntryService" will interact with these two services in the back. The user will authenticate against "MyEntryService". - Create a custom interceptor, which will intercept outgoing requests, fetch the existing "Authorization" header from httpContext, and append it to the outgoing request.
- Attach that interceptor only to "FoobarService". I.e., for "BazbarService" we do not wish to add this header.
I am fairly certain I need to implement an "AbpInterceptor" for this (or more specifically, override the DynamicHttpProxyInterceptor) and then I need to attach it specifically to FoobarService.
Something like this would probably work, though I am not sure how to add the headers to :
public class MyDynamicHttpProxyInterceptor : DynamicHttpProxyInterceptor
{
/private readonly IHttpContextAccessor _httpContextAccessor;
public MyDynamicHttpProxyInterceptor(
DynamicHttpProxyInterceptorClientProxy<TService> interceptorClientProxy,
IOptions<AbpHttpClientOptions> clientOptions,
IProxyHttpClientFactory httpClientFactory,
IRemoteServiceConfigurationProvider remoteServiceConfigurationProvider,
IApiDescriptionFinder apiDescriptionFinder
//IHttpContextAccessor httpContextAccessor
) : base(interceptorClientProxy, clientOptions, httpClientFactory, remoteServiceConfigurationProvider, apiDescriptionFinder)
{
_httpContextAccessor = httpContextAccessor;
}
public override async Task InterceptAsync(IAbpMethodInvocation invocation)
{
// get header from incoming context
string authorizationHeader = httpContextAccessor.HttpContext.Request.Headers["Authorization"];
// get outgoing context
var context = new ClientProxyRequestContext(
await GetActionApiDescriptionModel(invocation),
invocation.ArgumentsDictionary,
typeof(TService));
// TODO, do stuff to add headers to `context`
// Call the base interception method to do the rest
base.InterceptAsync(invocation)
}
}
I am not sure how exactly to add headers to the "outgoing" context. This probably needs to be added to context.Arguments someone, but it is unclear to me how.
Having the resolved the above, I then also need a way to bind MyDynamicHttpProxyInterceptor to the "Foobar" proxy.