在VB.NET程序中,我想从文件系统读取文件,然后使用不同的凭据将这些文件的压缩版本写入远程,安全的文件共享。
在cmd提示符下的类似操作是:
net use s: \\server\share /user:foo P@ssw0rd copy a+b | compress > s:\foo.bin net use s: /delete
这可能吗? 怎么样? (不要担心压缩和文件I / O,我的兴趣在安全部分。)
我是否使用WindowsImpersonationContext
做到这一点?
编辑 :你是对的,我真的不想映射驱动器; 我想要做的是使用不是默认凭据的凭据访问共享。 该应用程序由各种用户运行,他们没有正常的写入权限。 只是为了这个单个文件的目的,我想让用户写入共享。
那么如何使用备选凭证将单个文件写入共享? 请记住,我需要默认凭据或标识来读取作为压缩input的文件。
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
这是有道理的吗?
我知道我可以直接在共享上创build一个文件。 问题是如何使用替代凭据来做到这一点? 我知道如何在创build共享时传递alt信誉,这就是为什么我介绍了创build共享的想法。 我并不需要共享,因为它只对一个文件完成,而且只在一个程序中完成。
我知道我可以先创build文件,然后将文件复制到共享。 我不想这样做,因为它是一个大文件,我想stream一次。
您不需要映射驱动器。 您可以直接创建文件\\ server \ share \ foo.bin。
但是,如果你真的想,这里是一些代码:
从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