Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Imports Microsoft.VisualBasic
<Assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode:=True), _
Assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name:="FullTrust")>
Module Imperson
Public Class Impersonation
Private Declare Auto Function LogonUser 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 Boolean
<DllImport("kernel32.dll")> _
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource As IntPtr, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As [String], _
ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
End Function
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
Public Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, _
ByVal SECURITY_IMPERSONATION_LEVEL As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Boolean
' Test harness.
' If you incorporate this code into a DLL, be sure to demand FullTrust.
<PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
Public Overloads Shared Sub Main() 'ByVal args() As String
Dim tokenHandle As New IntPtr(0)
Dim dupeTokenHandle As New IntPtr(0)
Try
Dim userName, domainName, pasword As String
' Get the user token for the specified user, domain, and password using the
' unmanaged LogonUser method.
' The local machine name can be used for the domain name to impersonate a user on this machine.
domainName = Main_GUI.txt_domain.Text
userName = Main_GUI.txt_user.Text
pasword = Main_GUI.txt_pasword.Text
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
'This parameter causes LogonUser to create a primary token.
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
tokenHandle = IntPtr.Zero
' Call LogonUser to obtain a handle to an access token.
Dim returnValue As Boolean = LogonUser(userName, domainName, pasword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)
If False = returnValue Then
Dim ret As Integer = Marshal.GetLastWin32Error()
MsgBox("LogonUser failed with error code: ", ret)
Throw New System.ComponentModel.Win32Exception(ret)
Return
End If
Dim success As String
If returnValue Then success = "Yes" Else success = "No"
Main_GUI.txt_log.Text = "LogonUser succeeded? " & success & vbCrLf
Main_GUI.txt_log.Text = Main_GUI.txt_log.Text & vbCrLf & "Value of Windows NT token: " & tokenHandle.ToString()
' Check the identity.
Main_GUI.txt_log.Text = Main_GUI.txt_log.Text & vbCrLf & "Before impersonation: " & WindowsIdentity.GetCurrent().Name
' Use the token handle returned by LogonUser.
Dim newId As New WindowsIdentity(tokenHandle)
Dim impersonatedUser As WindowsImpersonationContext = newId.Impersonate()
' Check the identity.
Main_GUI.txt_log.Text = Main_GUI.txt_log.Text & vbCrLf & "After impersonation: " & WindowsIdentity.GetCurrent().Name
'
'!! Hier ist der Benutzer gewechselt worden (bis zum Undo)!
'
Dim pfad As String = "\\serverxxx\ccc\simresults\b1015\text.txt"
read_file(pfad, nothing)
' Hier wird wieder auf den ursprünglichen User zurückgewechselt
impersonatedUser.Undo()
' Check the identity.
Main_GUI.txt_log.Text = Main_GUI.txt_log.Text & vbCrLf & "After Undo: " + WindowsIdentity.GetCurrent().Name
' Free the tokens.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
CloseHandle(tokenHandle)
End If
Catch ex As Exception
Main_GUI.txt_log.Text = Main_GUI.txt_log.Text & vbCrLf & "Exception occurred. " & ex.Message
End Try
End Sub 'Main
End Class 'Class1
End Module