Results 1 to 7 of 7

Thread: Directory.Exists failing on cross domain call. LogonUser returns Token Zero

Threaded View

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Directory.Exists failing on cross domain call. LogonUser returns Token Zero

    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:
    1. Dim token As IntPtr = IntPtr.Zero
    2.             LogonUser("funkyD", "hydro", "AsIfIdPublishAPassword", 9, 0, token)
    3.             errorMessage = "About to impersonate"
    4.             Using person As WindowsImpersonationContext = New WindowsIdentity(token).Impersonate()
    5.                 errorMessage = "Impersonated"
    6.                 Try
    7.  
    8.                     ' Check folder exists
    9.                     If System.IO.Directory.Exists(databasePath) Then
    10.                         errorMessage = "DirectoryFound"
    11.  
    12.                     ... carry out some other file operations but these aren't being reached...
    13.  
    14.                 Catch Problem As Exception
    15.  
    16.                     errorMessage = Problem.Message
    17.                     ' Handle and return error code
    18.                     ProblemHandler(moduleLevelAquatorProccessID, Problem)
    19.                 Finally
    20.                     person.Undo()
    21.                     CloseHandle(token)
    22.                 End Try
    23.             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.
    Last edited by FunkyDexter; Nov 26th, 2021 at 10:06 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width