2009-04-19 102 views
10

我正在使用C#...使用远程管理凭据将文件复制到远程计算机

我需要能够将一组文件复制到大约500台独特的计算机。我已成功地使用LogonUser()方法来模拟具有复制文件所需权限的域帐户。对于文件的目标路径是这样的:

\\ RemoteComputer \ C $ \ SomeFolder

我的问题是...是有办法做到这一点,而无需使用一个全能域帐户(这些计算机未来可能未加入域)?我有每台计算机的本地管理员帐户...有没有一种简单的方法可以使用本地管理员帐户而不是域帐户将文件复制到计算机上?

回答

7

如果我错了,请纠正我,但您可以使用LogonUser模仿本地群组,也不只是域帐户。

From the net:

Imports System 
Imports System.Runtime.InteropServices 
Imports System.Security.Principal 
Imports System.Security.Permissions 
Public Class Form1 
    <DllImport("advapi32.DLL", SetLastError:=True)> _ 
    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _ 
     ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _ 
     ByRef phToken As IntPtr) As Integer 
    End Function 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim admin_token As IntPtr 
     Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent() 
     Dim wid_admin As WindowsIdentity = Nothing 
     Dim wic As WindowsImpersonationContext = Nothing 
     Try 
      MessageBox.Show("Copying file...") 
      If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then 
       wid_admin = New WindowsIdentity(admin_token) 
       wic = wid_admin.Impersonate() 
       System.IO.File.Copy("C:\right.bmp", "\\157.60.113.28\testnew\right.bmp", True) 
       MessageBox.Show("Copy succeeded") 
      Else 
       MessageBox.Show("Copy Failed") 
      End If 
     Catch se As System.Exception 
      Dim ret As Integer = Marshal.GetLastWin32Error() 
      MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString()) 
      MessageBox.Show(se.Message) 
     Finally 
      If wic IsNot Nothing Then 
       wic.Undo() 
      End If 
     End Try 
    End Sub 
End Class 
+1

你是正确的。我为logonType参数使用了不同的值,一旦我切换到LOGON32_LOGON_NEW_CREDENTIALS,它就像一个冠军!谢谢! – 2009-04-20 16:11:46

1

WNetAddConnection2将做的伎俩。只需使用空字符串作为本地设备名称,即可避免映射驱动器。完成后,您还需要确认并且close the connection。我将它包装到实现IDisposable的NetworkConnection类中。

相关问题