How to Impersonate a user for a file copy over the network when dns or netbios is not available

Posted by Scott Chamberlain on Stack Overflow See other posts from Stack Overflow or by Scott Chamberlain
Published on 2010-04-23T21:35:25Z Indexed on 2010/04/23 21:43 UTC
Read the original article Hit count: 802

Filed under:
|

I have ComputerA on DomainA running as userA needing to copy a very large file to ComputerB on WorkgroupB which has the ip of 192.168.10.2 to a windows share that only userB has write access to.

There is no netbios or dns resolving so the computer must be refrenced by IP

I first I tried

AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
WindowsIdentity UserB = new WindowsIdentity("192.168.10.2\\UserB", "PasswordB"); //Execption
WindowsImpersonationContext contex = UserB.Impersonate()
File.Copy(@"d:\bigfile", @"\\192.168.10.2\bifgile");
contex.Undo();

but I get a System.Security.SecurityException "The name provided is not a properly formed account name."

So I tried

AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
WindowsIdentity webinfinty = new WindowsIdentity("ComputerB\\UserB", "PasswordB"); //Execption

But I get "Logon failure: unknown user name or bad password." error instead.

so then I tried

IntPtr token;
bool succeded = LogonUser("UserB", "192.168.10.2", "PasswordB", LogonTypes.Network, LogonProviders.Default, out token);
if (!succeded)
{
     throw new Win32Exception(Marshal.GetLastWin32Error());
}
WindowsImpersonationContext contex = WindowsIdentity.Impersonate(token);
(...)
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
      string principal,
      string authority,
      string password,
      LogonTypes logonType,
      LogonProviders logonProvider,
      out IntPtr token);

but LogonUser returns false with the win32 error "Logon failure: unknown user name or bad password"

I know my username and password are fine, I have logged on to computerB as that user.

Any reccomandations

© Stack Overflow or respective owner

Related posts about c#

Related posts about impersonation