I'm trying to Impersonate a user thru code written in VB in an ASP page,
so that I can connect to a Samba server on a Linux system for storing
files.
The function LogonUSer(Win32 API call) fails when this is done. But, when
the same code is run in VB, it works.
Following are the things I have tried:
1) Removing the Anonymous Access from directory security tab of the
Website in IIS. This works but we cannot afford it as
we will be using Microsoft Challenge/Response services.
2) Adding the DLL that has Impersonation function to COM+/MTS. It doesnot
work.
3) Performing Step2 and adding the Administrator security context to the
Package. It doesnot work.
4) We have tried the suggestions given in query Q248187, Q223334 in MSDN
and microsoft support site. They don't seem to work either.
Could someone help!
The basic idea behind the whole exercise is to move away from the
Microsoft platform to Linux.
Code we used:
ASP
~~~
<%
dim o, f
set o = Server.CreateObject("Crypto.clsCrypto")
set f = Server.CreateObject("Scripting.FileSystemObject")
o.Impersonate "fileuser","samba","system1234"
f.CreateFolder "\\samba\permfiles\12"
f.CreateTextFile "\\samba\permfiles\12\new.txt"
o.EndImpersonate
%>
VB Component COde:
~~~~~~~~~~~~~~~~~~
Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal
hToken As Long) As Long
Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA"
(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal
lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider
As Long, phToken As Long) As Long
Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
Public Function Impersonate(ByVal sUserName As String, ByVal sDomain As
String, ByVal sPassword As String) As Boolean
Dim Result As Long
Dim hToken As Long
Dim lngLastErr As Long
Call RevertToSelf
Result = LogonUser(sUserName, sDomain, sPassword,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken)
Result = ImpersonateLoggedOnUser(hToken)
End Function
Thanks,
Gautam