Impersonation (change the user) in Windows Application

  • Thread starter Thread starter T. Beneke
  • Start date Start date
T

T. Beneke

I need in a Windowsapplication the access to a file that is on a Fileserver.
Only the administrator has access to the directory of the file.
My App needs to open this file. In ASP.net I can Impersonate the user, but
it doesn't work in a Windows App.
I tried to change the Principal of the thread but it doesn't work.
The Script is:

....
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_LOGON_NETWORK As Integer = 3 'tried both
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
Dim token As IntPtr
Dim WI As System.Security.Principal.WindowsIdentity
If LogonUser("administrator", "mydom", "password",
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
'Logonuser from API advapi32.dll
WI = New System.Security.Principal.WindowsIdentity(token) 'that works
End If



Dim WP As New System.Security.Principal.WindowsPrincipal(WI) 'that
works
'System.Threading.Thread.CurrentThread.CurrentPrincipal = WP

Dim NeuerThread As New Threading.Thread(AddressOf newSub)
NeuerThread.CurrentPrincipal = WP 'that works
NeuerThread.Start()
....

' in newSub was tried to get access to that file but it doesn't work

has anybody an idea whats wrong or an solution for this problem?

Thanks a lot Thomas
 
Yes I tried your code but it doesn't work at all
Your solution is similar to my first post and other examples on the
internet. An other user told me, that my code work on his environment. I
think I have a problem with the configuration of our Network, Domain or such
like that. The code work in asp.net but it won't work in Win Apps on
computer in our Network (W2003 Fileserver).

I have realy no idea why this code works in other networks but not in our.
:-((

Thomas
 
Back
Top