1

I have a requirement where I need to copy files to a remote server with credentials from a .Net 6.0 application hosted in a docker container. I can do this easily by impersonating the service account in VS (using WindowsIdentity.RunImpersonated) but this fails when application is running inside the docker container. I cannot access advapi32.dll from inside the container. So, I cannot use LogonUser to generate token handle to impersonate the service account (which has access to the remote server).

Is there a way I can use impersonation inside docker container without being dependent on advapi32.dll OR if I can copy the files to remote server without using impersonation at all.

I tried the following C# code which works locally but not inside a container

 [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
        int dwLogonType, int dwLogonProvider, out SafeAccessTokenHandle phToken);


bool returnValue = LogonUser(username, domainname, password,
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
            out safeAccessTokenHandle);


WindowsIdentity.RunImpersonated(
                safeAccessTokenHandle,
                // User action  
                () => {
                    // Check the identity.  
                    Console.WriteLine("During impersonation: " + WindowsIdentity.GetCurrent().Name);
               });
pal
  • 11
  • 4
  • You can not use windows dlls in a linux container. Either use a windows container or use a cross platform solution. – Eldar Dec 13 '22 at 09:14
  • What does your Dockerfile look like? Is your use of Docker configured to target Windows containers? That will be required in order to make use of Windows APIs within a Docker container. – Matt Thalman Dec 13 '22 at 13:51

0 Answers0