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);
});