'How do I get cookie correctly?

How do I get a cookie in the correct way, because the API which I want to use needs a cookie, so first I need to grab it using the post method for example

 _httpClient.SendAsync(request, token);

In the Authentication class I've got a static method where I am using httpClient:

var client = new HttpClient()

I tried to register it in Services like this:

services.AddHttpClient("Person", async (provider, client) =>
{
    var config = new PersonConfiguration(provider.GetRequiredService<IConfiguration>());
    var cookie = await Authentication.GetCookie(config);

    client.BaseAddress = new Uri(config.Link);
    client.DefaultRequestHeaders.Add("Cookie", cookie.Value);
})

but I am not entirely sure I should use async operation in lambda expresion. Any suggestions how I can solve that problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source