내가 틀렸다면 수정하십시오. 도메인 계정뿐만 아니라 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
당신은 정확합니다. 작동하지 않는 logonType 매개 변수에 대해 다른 값을 사용하고있었습니다 ... 일단 LOGON32_LOGON_NEW_CREDENTIALS로 전환하면 해당 매개 변수가 챔피언처럼 작동합니다! 감사! –
나는 이것이 관리자 암호가 실행중인 컴퓨터와 원격 컴퓨터에서 동일 할 때만 작동한다고 생각합니다. "LogonUser 함수는 사용자를 로컬 컴퓨터에 로그온하려고 시도하며 로컬 컴퓨터는 LogonUser가 호출 된 컴퓨터이며 LogonUser를 사용하여 원격 컴퓨터에 로그온 할 수 없습니다." –
위의 코드에서 LogOnUser Function에 로컬 관리자 이름을 사용자 이름으로, 로컬 컴퓨터 이름을 도메인으로, 암호를 암호로 제공하십시오. 이러한 모든 자격 증명은 대상 컴퓨터 (또는 서버)입니다. 작동합니다. 고마워요! –