0

I have a file that is being generated automatically on a server in a Windows domain, say, called, "prod" and I will need to have VB.NET to transmit this file to another server in another Windows domain, say, "QA", where QA and Prod have different credentials altogether and I have to authenticate that credential every time I opened up the destination folder.

Therefore, I guess the regular filecopy method would not work, is there another way where we can accomplish this?

Thanks a lot!

AZhu
  • 1,312
  • 6
  • 22
  • 40
  • 3
    Don't solve this in your code, storing username/passwords in your code or config data is fundamentally flawed. Let the LAN admin sort this out with external trust, he can ask about it at serverfault.com. http://technet.microsoft.com/en-us/library/cc787646%28WS.10%29.aspx – Hans Passant Mar 12 '11 at 17:56

2 Answers2

1

How about something like this

<DllImport("advapi32.dll")> _
Public Shared Function LogonUser(lpszUsername As String, lpszDomain As String, lpszPassword As String, dwLogonType As Integer, dwLogonProvider As Integer, phToken As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll")> _
Public Shared Function CloseHandle(hObject As IntPtr) As Boolean
End Function

Public Shared Function OpenFileWithAccount(filename As String, username As String, domain As String, password As String) As Stream
    Dim token As IntPtr
    If Not LogonUser(username, domain, password, 2, 0, token) Then
        Throw New Win32Exception()
    End If
    Try
        Using WindowsIdentity.Impersonate(token)
            Return File.OpenRead(filename)
        End Using
    Finally
        CloseHandle(token)
    End Try
End Function

and call

Dim stream as Stream
stream = OpenFileWithAccount("filePath","userName","prod","password")
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I actually got this error when trying to run the code: System.AccessViolationException was caught Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt." any suggestion? – AZhu Mar 23 '11 at 16:43
-1

token = Marshal.AllocHGlobal(8)

Pablo
  • 1