VB.NET更改域用户身份复制文件到域中的共享目录

Standard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Imports System.Management
Imports System.Security.Principal
 
Module modTemp
 
  Dim WITH_PROFILE As Integer = 5
  Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
  Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
  Const LOGON32_LOGON_NETWORK As Long = 3
 
  Dim impersonationContext As WindowsImpersonationContext
 
  Declare Function LogonUserA Lib "advapi32.dll" (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
 
  Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
              ByVal ExistingTokenHandle As IntPtr, _
              ByVal ImpersonationLevel As Integer, _
              ByRef DuplicateTokenHandle As IntPtr) As Integer
 
  Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
  Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
 
  Public Function impersonateValidUser(ByVal userName As String, ByVal domain As String, _
  ByVal password As String) As Boolean
 
    Dim tempWindowsIdentity As WindowsIdentity
    Dim token As IntPtr = IntPtr.Zero
    Dim tokenDuplicate As IntPtr = IntPtr.Zero
    impersonateValidUse* = **lse
 
    If RevertToSelf() Then
      If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, _
        LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
        If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
          tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
          impersonationContext = tempWindowsIdentity.Impersonate()
          If Not impersonationContext Is Nothing Then
            impersonateValidUser = True
          End If
        End If
      End If
    End If
    If Not tokenDuplicate.Equals(IntPtr.Zero) Then
      CloseHandle(tokenDuplicate)
    End If
    If Not token.Equals(IntPtr.Zero) Then
      CloseHandle(token)
    End If
  End Function
 
  Public Sub undoImpersonation()
    impersonationContext.Undo()
  End Sub
 
End Module
 
  'www.yemaosheng.com
 
  Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
    If impersonateValidUser("username", "domainname", "password") Then
      Call ToCopyFile()
    Else
      MsgBox("Impersonation failed")
    End If
  End Sub
 
  Private Sub ToCopyFile()
    Try
      Dim strSource As String
      strSource = "C:\Documents and Settings\yemaosheng\桌面\test.txt"
 
      Dim strTarget As String
      strTarget = "\\domain-svr\temp\test_ye.txt"
 
      System.IO.File.Copy(strSource, strTarget, True)
 
    Catch ex A* **ception
      MsgBox(ex.Message.ToString, , "提示")
    End Try
  End Sub