ASP.Net Core Web Application Unable to Create SSL Connection With API When Both Are Run In Docker

Viewed 571

I have an ASP.Net web application and a separate ASP.Net API that I have created using .Net 5 that both need to be run in Docker, but as separate applications. Both Docker images run fine on their own, and I can hit the API while it's running in Docker with Postman without any issues. The problem comes when I try to send a request to the API in the web application using an HttpClient with this code:

    static readonly HttpClient client = new HttpClient();

    private async Task<ApplicationUser> LdapRest(string apiUrl, LoginPostModel postObject)
    {
        ApplicationUser user = null;
        try
        {
            HttpResponseMessage response = await client.PostAsync(apiUrl, postObject, new JsonMediaTypeFormatter()).ConfigureAwait(false);
            //response.EnsureSuccessStatusCode();
            if (response.IsSuccessStatusCode)
            {
                await response.Content.ReadAsStringAsync().ContinueWith((Task<string> x) =>
                {
                    if (x.IsFaulted) throw x.Exception;
                    user = JsonConvert.DeserializeObject<ApplicationUser>(x.Result);
                });
            }
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "LdapRest Failed");
        }
        return user;
    }

I get the error:

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors

I believe this stems from using the app.UseHttpsRedirection(); middleware, which is required for this application. I'm using a self-signed cert using the dotnet dev-certs https command and referencing it in both containers with environment variables for Kestrel. I tried using the method outlined here, where you create a bridge network in Docker and having both images on it and passing in the URI for the API as an environment variable using the API's container name, but it hasn't worked for me, and I'm struggling to find any other workable solutions to fix this issue that aren't "don't use https redirection".

0 Answers
Related