我正在尝试将文件从Server A
复制到Server B
。
所以基本上,它快速而简单,File.Copy
。
File.Copy("\\ServerA\c$\CopyImageTest\Server61.txt", "\\ServerB\c$\CopyImageTest\Server61.txt")
File.Copy("\\ServerB\c$\CopyImageTest\Server182.txt", "\\ServerA\c$\CopyImageTest\Server182.txt")
由于Server A
需要登录才能使用,很明显我收到了这个错误:
System.IO.IOException:登录失败:未知用户名或密码错误。
我环顾四周,发现我应该使用Impersonation
来访问该服务器并复制文件。我发现C#
位于VB.NET
,并将其转换为Module Module1
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Sub Main()
Try
'Some declarations omitted for clearance.
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const LOGON32_LOGON_NEW_CREDENTIALS As Integer = 9
Try
Dim bImpersonated As Boolean = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, pExistingTokenHandle)
If (Not bImpersonated) Then
Dim nErrorCode As Integer = Marshal.GetLastWin32Error()
Return
End If
Dim bRetVal As Boolean = DuplicateToken(pExistingTokenHandle, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, pDuplicateTokenHandle)
If (Not bRetVal) Then
Dim nErrorCode As Integer = Marshal.GetLastWin32Error()
CloseHandle(pExistingTokenHandle)
Return
Else
Dim newId As WindowsIdentity = New WindowsIdentity(pDuplicateTokenHandle)
Dim impersonateduser As WindowsImpersonationContext = newId.Impersonate()
'Shouldn't I be copying now?
File.Copy("\\ServerA\c$\CopyImageTest\Server61.txt", "\\ServerB\c$\CopyImageTest\Server61.txt")
File.Copy("\\ServerB\c$\CopyImageTest\Server182.txt", "\\ServerA\c$\CopyImageTest\Server182.txt")
sResult = sResult & "After impersonation: " & WindowsIdentity.GetCurrent.Name & Environment.NewLine
impersonateduser.Undo()
End If
Catch ex As Exception
'Omitted
Finally
If (Not pExistingTokenHandle = IntPtr.Zero) Then
CloseHandle(pExistingTokenHandle)
End If
If (Not pDuplicateTokenHandle = IntPtr.Zero) Then
CloseHandle(pDuplicateTokenHandle)
End If
End Try
Catch ex As Exception
'Omitted
End Try
End Sub
' DLL Imports and Enum Omitted.
End Module
。
这是我正在使用的代码:
User was able to login. (This is used after LogonUser()) (Previously it was throwing an error (1326) Logon failure: unknown user name or bad password)
Before impersonation: User-PC\User (Result of WindowsIdentity.GetCurrent().Name; Before Impersonate())
After impersonation: User-PC\User (Result of WindowsIdentity.GetCurrent().Name; After Impersonate())
System.IO.IOException: Logon failure: unknown user name or bad password.
输出结果的一些代码已被省略,但这是控制台的结果:
Logon failure: unknown user name or bad password.
令人困惑的是,模仿前后的名称与我的本地PC相同。但登录工作正常。 (我对此持肯定态度,因为如上所述,它之前失败了。)
其次,文件仍然没有像以前一样被复制:
Impersonation
这实际上是Server A
应该如何工作,还是我错过了一些明显的东西?
在进一步挖掘之后,我还找到了this answer,我在答案中使用了相同的确切代码,但根本没有运气。
有关我尝试访问的\\ServerA\c$\CopyImageTest
的一些信息并从中复制文件:
File.Copy("\\ServerA\c$\CopyImageTest\Server61.txt", "C:\CopyImageTest\Server61.txt")
File.Copy("C:\CopyImageTest\Server182.txt", "\\ServerA\c$\CopyImageTest\Server182.txt")
。@ Esko的评论指导我做两到三次测试。
以下是该方案:
通过模拟,我尝试了以下代码:
ServerA
这意味着我只将文件从File.Copy("C:\CopyImageTest\Server61.txt", "\\ServerB\c$\CopyImageTest\Server61.txt")
File.Copy("\\ServerB\c$\CopyImageTest\Server182.txt", "C:\CopyImageTest\Server182.txt")
复制到本地,反之亦然
结果:通过。
使用模拟
ServerB
这意味着我尝试从File.Copy("C:\CopyImageTest\Server61.txt", "\\ServerB\c$\CopyImageTest\Server61.txt")
File.Copy("\\ServerB\c$\CopyImageTest\Server182.txt", "C:\CopyImageTest\Server182.txt")
复制到本地和副本verca
结果:失败
没有冒充
ServerB
复制ServerA
和。{
结果:通过
所以最后,我认为问题是当模拟用户访问ServerB
时,它无法访问ServerB
,因为知道username
不需要password
} ServerB
尝试从我的电脑访问它时。 也许是因为我的电脑和ServerA
在同一网络上使用相同的域但ServerA
在同一网络上使用了另一个域?
是否有任何解决方法可以通过单一模拟访问两台服务器?
注意:我知道能够将文件从ServerB
复制到本地,然后撤消模拟并复制到<el-form-item label="Description"
:prop="'items.' + index + '.description'"
:rules="{required: true, message: 'description is required', trigger: 'blur'}">
<el-input v-model="item.description"></el-input>
</el-form-item>
,我只是想寻找更直接的方式