Hi All
I've got an application that needs to access the file system on a remote machine. The machine that's running the code is not on the same domain as the remote machine and therefore needs to pass credentials in order to carry this out. I found this post on Stack Overflow and have tried to copy it. Here's my resultant code:-
VB Code:
Dim token As IntPtr = IntPtr.Zero LogonUser("funkyD", "hydro", "AsIfIdPublishAPassword", 9, 0, token) errorMessage = "About to impersonate" Using person As WindowsImpersonationContext = New WindowsIdentity(token).Impersonate() errorMessage = "Impersonated" Try ' Check folder exists If System.IO.Directory.Exists(databasePath) Then errorMessage = "DirectoryFound" ... carry out some other file operations but these aren't being reached... Catch Problem As Exception errorMessage = Problem.Message ' Handle and return error code ProblemHandler(moduleLevelAquatorProccessID, Problem) Finally person.Undo() CloseHandle(token) End Try End Using
This is resulting in an error message "Token Cannot be Zero". I've checked Marshal.GetLastWin32Error and it's return 1348 but I'm not sure where to find a reference to look this up.
Can anyone suggest what I'm getting wrong here?
Some more context that may be relevant: The actual setup here is that the above code is in a service. This service is being run on a virtual machine under the local system account. The remote machine it's reading from is actually the machine that's hosting the virtual machine but the virtual machine does not belong to the domain the host machine is on. This is a fairly unusual setup that's really just for me to do development to test this stuff out but, when we roll out the product, it won't be unusual for customers to spin up virtual machines of this nature and then want to call out to pick up files from their main company network and the VM is unlikely to be part of that domain.




Reply With Quote