none
Store files to memory for transfer? RRS feed

  • Question

  • I dont know if this is possible, but here goes.

    I am using the impersonate method to access files on one of our networks and need to copy them to another network.

    Catch is I need one set of credentials to access the files and another set to copy the files..........all while the client workstation is on another set of credentials.

    Example:

    I am running my application from NET01, the files I want to copy are on NET02 and I need to copy them to NET03, all use different credentials and the NET01 the end user is not a local administrator.

    With that stated, is there a method to copy lets say and Access MDB into the memory of the running applications, change my impersonate credentials and then copy it from memory to the other network.

    My code for impersonate works, am not looking for that, but need a method or concept to copy into a location that can be accessed by more then one set of credentials.

    Thursday, March 15, 2018 1:08 PM

All replies

  • Previously in the Visual FoxPro app my VB.NET app is replacing this was done via mapped drives, they no longer want mapped drives as client workstations vary to much for drive letters.
    Thursday, March 15, 2018 1:09 PM
  • Ryan,

    Internet is not anymore a place of only good willing persons. Therefore the possibilities to sent (or place) data becomes everytime less. 

    But downloading a file on a computer is very easy, although the end user has to start it. Mostly placing the files of a server and a simple email is then enough. 


    Success
    Cor

    Thursday, March 15, 2018 1:28 PM
  • With that stated, is there a method to copy lets say and Access MDB into the memory of the running applications, change my impersonate credentials and then copy it from memory to the other network.

    Using your first set of credentials, use a FileStream opened for reading (eg from IO.File.Open) to read all of the file data bytes into an array.  Then you can use your second set of credentials to open a FileStream for writing and write the byte array out to this new file.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, March 15, 2018 1:33 PM
    Moderator
  • Also, if you are working with large files (eg gigabytes in size) then you can open both reading and writing streams, each with their own credentials, and then read and write the file in chunks instead of reading the entire file into memory and then writing it back out.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, March 15, 2018 1:35 PM
    Moderator
  • This sounds like a plan Kimble, I will provide feedback on this. I am using the file stream copy method now but not to an array, am going from point A to B so the array would be an easy implementation if I can get it to work.........
    Thursday, March 15, 2018 1:38 PM
  • Thank you Cor, but the networks are all local, just different domains with no email access on the two end points.
    Thursday, March 15, 2018 2:11 PM
  • This sounds like a plan Kimble, I will provide feedback on this. I am using the file stream copy method now but not to an array, am going from point A to B so the array would be an easy implementation if I can get it to work.........

    In theory it should be something along the lines of:

    ' using NuGet package SimpleImpersonation (https://github.com/mj1856/SimpleImpersonation)
    Public Sub CopyFiles(sourcePath As String, sourceCredentials As Net.NetworkCredential, destinationPath As String, destinationCredentials As Net.NetworkCredential)
        Dim fileBytes() As Byte
        Using SimpleImpersonation.Impersonation.LogonUser(sourceCredentials.Domain, sourceCredentials.UserName, sourceCredentials.Password, SimpleImpersonation.LogonType.NetworkCleartext)
            fileBytes = IO.File.ReadAllBytes(sourcePath)
        End Using
        Using SimpleImpersonation.Impersonation.LogonUser(destinationCredentials.Domain, destinationCredentials.UserName, destinationCredentials.Password, SimpleImpersonation.LogonType.NetworkCleartext)
            IO.File.WriteAllBytes(destinationPath, fileBytes)
        End Using
    End Sub
    

    This is using the SimpleImpersonation NuGet package.  I don't know if that is the correct LogonType or not, but use whatever you are using now that works.

    If you end up needing to copy the files in chunks due to file size then this could get more complicated to maintain both impersonations while reading and writing the files.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, March 15, 2018 2:21 PM
    Moderator