views:

353

answers:

2

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

A: 

Out of my head, did you try

user@computer

instead of

computer\user

?

Daniel Mošmondor
Logon failure: unknown user name or bad password.
Scott Chamberlain
+1  A: 

Might my answer here apply to what you're attempting?

Jesse C. Slicer
It would appear that my 3rd way (very close to what you did) is the correct way. Using your code it worked fine.
Scott Chamberlain