|
Executing APG Scripts in Active Server Pages |
|
See Also: |
File permissions are an important consideration when executing an APG script in an ASP page. Normally ASP pages do not have file write permissions in the web site directory. Appropriate file permissions should be set according to the needs of the web application.
When running an APG script from an ASP page, ensure the APG script has proper create and write file permissions, for both Output files and Log files. When an APG script is run from an ASP page, the APG script runs in the same security context as the ASP page. So, if the ASP security context (normally IUSR_MachineName) does not have file write permissions, by default the output or log file will not be generated.
If you run an APG script from an ASP page using default security settings, an error may occur when content is first written to the output file:

This error occurs when APGen tries to create or write a file using the anonymous user security context. By default, ASP pages are run in the anonymous user (IUSR_MachineName) security context. And by default, IUSR_MachineName does not have permissions to create and write files in web server directories.
There are several solutions to this problem:
APGen.Logon,
Output.Logon, and/or Log.Logon objects to specify the user account
used to create/write the file. Ensure that
this account has Write permissions in the output and log directories.
This solution is discussed in detail below.
Any of these solutions can meet the security needs of generating files from ASP. If all users should be able to create and modify web pages using APGen via ASP, use solutions 2, 3 or 4. If only select users can create and modify web pages (and if you want to use Windows authentication), use solution number 1.
File permissions for log files are not as critical as
file permissions for output files. If a log file cannot be written to, the
log entry is written to the system Event Log. You can also specify that
all errors be written to the Event Log (regardless of file permissions)
by setting Log.Flags to
apgEventLogErrors or apgEventLogFatalErrors.
The Logon object is
used to connect to a network share, or to impersonate a user when creating local
files. Usage is straightforward. This ASP page runs an APG script,
and the output file is generated using the APGen_User account:
<%
Option Explicit
' Create an APGen object
Dim oAPGen
Set oAPGen =
Server.CreateObject("APGen")
' Set the logon properties
oAPGen.Logon.User =
"APGen_User"
oAPGen.Logon.Password = "pass"
oAPGen.Logon.Domain = "domain"
' Set the output directory
oAPGen.OutputDir = "\\wg1\share\" ' network
share<
BR >
' Execute the APG script
oAPGen.Run Server.MapPath("default.apg")
%>
<h2>APG Script Executed</h2>
There are a few things to be aware of when using the
Logon object in an ASP page:
LogonUser() and ImpersonateLoggedOnUser() are used to impersonate
the user before writing the file. LogonUser() requires the process to have the "Act as
part of the operating system" privilege, otherwise an Access Denied error is returned. IIS runs in
the SYSTEM account, which has this privilege. Out-of-process web
applications are run in the IWAM_MachineName account, which does not have this
privilege (see Q232513 - PRB: LogonUser Fails in ISAPI Extensions
). Thus impersonation does not work in out-of-process web
applications.
WNetAddConnection2() is used to connect to the network share before writing
the file. WNetAddConnection2() does not require the "Act as
part of the operating system" privilege, so it works in out-of-process web
applications. When using APGen and the Logon object in an ASP page, we
recommend that you write to network shares instead of to local
directories. Even network shares on the local computer can be used.
' Set the logon properties
oAPGen.Logon.User = "APGen_User"
oAPGen.Logon.Password = "pass"
oAPGen.Logon.Domain = "domain"
' Set the output directory
oAPGen.OutputDir = "\\wg1\share\" ' network share - GOOD!
or
' Set the logon properties
oAPGen.Logon.User = "APGen_User"
oAPGen.Logon.Password = "pass"
oAPGen.Logon.Domain = "domain"
' Set the output directory
oAPGen.OutputDir = "\\test_srv\share\" ' local share -
GOOD!
Using the Logon object
while writing to a local drive can be problematic if the web application is run
outside of the IIS process:
' Set the logon properties
oAPGen.Logon.User = "APGen_User"
oAPGen.Logon.Password = "pass"
oAPGen.Logon.Domain = "domain"
' Set the output directory
oAPGen.OutputDir = Server.MapPath("output") ' local drive -
May be a problem!!
' IMPORTANT: If this ASP page is run in an out-of-process
web
' application, logon and
impersonation will fail.
For additional information, see the Security topic.