Cannot reach LDAP server from Windows Server docker container

Viewed 23

I have the following code that uses an LDAP address to find a user via AD & the groups the user belongs to.

using var ldapContext = new DirectoryEntry(_configuration["ActiveDirectory:Path"], username, password);
using var searcher = new DirectorySearcher(ldapContext);
searcher.Filter = $"(&(objectCategory=person)(objectClass=user)(sAMAccountName={username}))"; 
searcher.PropertiesToLoad.Add("memberOf");
      try
        {
            List<string> groups = new();
            foreach (SearchResult entry in searcher.FindAll())
                if (entry.Properties.Contains("memberOf"))
                  ...

Running this works fine, until I try running it in a Docker container. Then I get the simple error message: "The server is not operational." The DC server is reachable from the container.

The Dockerfile is the standard one created by the VS template, but with the Windows Server Core tag.

FROM mcr.microsoft.com/dotnet/aspnet:6.0.9-windowsservercore-ltsc2022 AS base
EXPOSE 80
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0.401-windowsservercore-ltsc2022 AS build
WORKDIR /src
COPY ["src/services/identity/Identity.Api.Service/Identity.Api.Service.csproj", "src/services/identity/Identity.Api.Service/"]
RUN dotnet restore "src/services/identity/Identity.Api.Service/Identity.Api.Service.csproj"
COPY . .
WORKDIR "/src/src/services/identity/Identity.Api.Service"
RUN dotnet build "Identity.Api.Service.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Identity.Api.Service.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Identity.Api.Service.dll"]

Does it have to run under another image or is there some kind of extra configuration that needs to be done when running in Docker?

Complete StackTrace:

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
in /_/src/libraries/System.DirectoryServices/src/System/DirectoryServices
/DirectoryEntry.cs:line 561

at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) 
in /_/src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectorySearcher.cs:line 623

at Identity.Api.Service.Controllers.IdentityController.LoginViaActiveDirectory(String username, String password)
1 Answers

Turns out the issue was host related, when I tried running the service as a docker container in the production environment it worked just as expected.

I suspect that there's some underlying host issue since my own machine is running Docker via WSL2 & in production it is running natively on Windows Server 2022.

Related