C# 使用远程管理员凭据将文件复制到远程计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/766033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy file to remote computer using remote admin credentials
提问by Casey Gay
I am using C#...
我正在使用 C#...
I need the ability to copy a set of files to about 500 unique computers. I have successfully been able to use the LogonUser() method to impersonate a domain account that has the required permissions to copy the files. The destination path for the files is something like:
我需要能够将一组文件复制到大约 500 台不同的计算机。我已经成功地能够使用 LogonUser() 方法来模拟具有复制文件所需权限的域帐户。文件的目标路径类似于:
\\RemoteComputer\C$\SomeFolder
\\远程计算机\C$\SomeFolder
My questions is...is there a way to do this without having to use an all-powerful domain account (these computers may not be joined to the domain in the future)? I have the local administrator accounts for every computer...is there a simple way to copy a file to a computer using it's LOCAL administrator account rather than a domain account?
我的问题是......有没有办法在不必使用全能域帐户的情况下做到这一点(这些计算机将来可能不会加入域)?我有每台计算机的本地管理员帐户......是否有一种简单的方法可以使用它的本地管理员帐户而不是域帐户将文件复制到计算机?
采纳答案by Shay Erlichmen
Correct me if I'm wrong, but you can use LogonUserto impersonate a local group also not only domain accounts.
如果我错了,请纠正我,但您可以使用LogonUser来模拟本地组,而不仅仅是域帐户。
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
回答by Mark Brackett
WNetAddConnection2will do the trick. Just use an empty string for the local device name, to avoid mapping a drive. You also want to make sure and close the connectionwhen you're done. I wrap it into a NetworkConnection class that implements IDisposable.
WNetAddConnection2可以解决问题。只需对本地设备名称使用空字符串,以避免映射驱动器。您还想确保并在完成后关闭连接。我将它包装到一个实现 IDisposable 的 NetworkConnection 类中。