views:

388

answers:

1

Within a VB.NET program, I want to read files from a filesystem, then write a compressed version of those files to a remote, secure fileshare, using different credentials.

The analogous operation at the cmd prompt is:

net use s: \\server\share /user:foo P@ssw0rd
copy a+b | compress > s:\foo.bin
net use s: /delete

Is this possible? How? (don't worry about the compression and file i/o. My interest is in the security parts.)

Do I do it with WindowsImpersonationContext ?

EDIT: you're right, I don't really want to map a drive; what I want to do is access a share with credentials that are not the default credentials. The app gets run by all sorts of users, and they don't have write access to the share normally. Just for the purposes of this single file, I want to allow the users to write to the share.

So how do I write a single file to a share, using alternative credentials? Keep in mind that I need the default credentials or identity to read the files that act as input to the compression.

 UserX reads files a1 and b1 as UserX, writes file c1 as UserA 
 UserY reads files a2 and b2 as UserY, writes file c2 as UserA

Is this making sense?

I know I can create a file on a share directly. The issue is how to do that with alternative credentials? I know how to pass alt creds when creating a share, which is why I introduced the idea of creating a share. I don't really need the share, because it is done only for a single file, and only within a program.

And I know I could create the file first, then copy the file to the share. I don't want to do that because it's a large file and I'd like to stream it once.

+2  A: 

You don't need to map a drive. You can just create the file \\server\share\foo.bin directly.

But, if you really want to, here is some code:

From http://www.mredkj.com/vbnet/vbnetmapdrive.html

     Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
( ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

     Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer

        <StructLayout(LayoutKind.Sequential)> _
    Public Structure NETRESOURCE
            Public dwScope As Integer
            Public dwType As Integer
            Public dwDisplayType As Integer
            Public dwUsage As Integer
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
        End Structure

    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1

    Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean

            Dim nr As NETRESOURCE
            Dim strUsername As String
            Dim strPassword As String

            nr = New NETRESOURCE
            nr.lpRemoteName = UNCPath
            nr.lpLocalName = DriveLetter & ":"
            strUsername = Nothing '(add parameters to pass this if necessary)
            strPassword = Nothing '(add parameters to pass this if necessary)
            nr.dwType = RESOURCETYPE_DISK

            Dim result As Integer
            result = WNetAddConnection2(nr, strPassword, strUsername, 0)

            If result = 0 Then
                Return True
            Else
                Return False
            End If
        End Function

    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
        Dim rc As Integer
            rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

            If rc = 0 Then
                Return True
            Else
                Return False
            End If

        End Function
BobbyShaftoe
I know I can create a file on a share directly. The issue is how to do that with alternative credentials? I know how to pass alt creds when creating a share, which is why I introduced that. I don't really need the share, because it is done only for a single file, and only within a program.
Cheeso