Impersonation (change the user) in Windows Application

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
 
H

Herfried K. Wagner [MVP]

T

T. Beneke

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
 
T

T. Beneke

I got it.

It worked only with UNC Path but not with mapped drives (S:\temp...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top