Skip to content

5. Authentication

Simon Heiss edited this page Jul 27, 2019 · 2 revisions

Authentication

If you secure your services, Microwave has to know how to authenticate itself when calling another service to get the domain events or discover the service map. To do that, there is a factory interface IMicrowaveHttpClientFactory that you can implement to create the http clients the way you have to, to be able to authenticate on a call to the other service. The passed URI is always the base address of the service Microwave wants to call, so you can decide how to authenticate with this service. You do not have to set the BaseAdress, though, Microwave will do that for you later on. An implementation could look like this:

public class MyMicrowaveHttpClientFactory : IMicrowaveHttpClientFactory
{
    public Task<HttpClient> CreateHttpClient(Uri serviceAdress)
    {
        var discoveryClient = new HttpClient();
        discoveryClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("123");
        return Task.FromResult(discoveryClient);
    }
}

Then pass the class as an instance to the MicrowaveConfiguration in the startup to tell microwave to use this factory when making a remote service call. This instance is registered as singleton.

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMicrowave(config =>
    {
        config.WithHttpClientFactory(new MyMicrowaveHttpClientFactory());
    });
}
Clone this wiki locally