Re: SecurityException trying to create an event source from ASP.NET

From: Jeff Cummins (cummins@csdg.com)
Date: 11/18/02


From: "Jeff Cummins" <cummins@csdg.com>
Date: Mon, 18 Nov 2002 09:05:48 -0500


The problem is that in order to create an event source, the program
attempting to do call WriteEntry needs to create a new subkey in this
registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\

The subkey is the name of your application (ie - the Event Source).

The ASPNET account that ASP.NET uses by default does not have permission to
do this.

There are three solutions I know of:

1. Run ASP.NET as SYSTEM, or another privileged account. This can be done
by changing the <processModel> tag in the Machine.config. SYSTEM is
basically a local admin account, however, so if anyone hijacks your ASP.NET
process, they could basically do anything on that server, so this is a
security hole.

2. Add an installer project for your web application and include an
addition to the registry key above so that the event source is created when
your application is installed.

3. Work out a way to temporarily impersonate a privileged account just to
create the event source and then revert to running as ASPNET. This can be
done using a COM+ serviced component, but this is a little complicated and
might be overkill just to create an event source. The approach I used was
to use the WindowsImpersonationContext class to access the LogonUser method
of the Windows API. There's example code in the documentation for
WindowsImpersonationContext:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemSecurityPrincipalWindowsImpersonationContextClassTopic.asp

The latter seems like the most elegant approach to me, but there are still a
few bugs to be worked out - for instance, on Windows 2000, you need a
special permission to call LogonUser, which ASPNET doesn't have by default.

Good luck.

Jeff Cummins

"Richard Dixson" <richard_dixson5@hotmail.com> wrote in message
news:af4bc285.0211160848.272a3e2d@posting.google.com...
> I wrote a class in C# that I call from a ASP.NET page. During a
> method call into my class, I want to write an event into the
> Application event log.
>
> I do this with the following code:
>
> m_eventLog = new EventLog();
> m_eventLog.Source = "MyProgram";
> m_eventLog.WriteEntry("My log message");
>
> However I am getting the SecurityException which is listed below. It
> apparently is occurring because it is the first time I am trying to
> log an event for this source, and is trying to register the source.
>
> What can I do so that the event source gets registered properly. Note
> that in my case I am distributing a component used from ASP.NET and do
> not have control over the machine it is running from. So somehow I
> have to handle this situation where my code is smart enough to get
> this registered when the code is executing from the context of an
> anonymous IIS account (IUSR_xxxxx).
>
> Below is the stack trace for the error. Thanks!
>
> [SecurityException: Requested registry access is not allowed.]
> Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean
> writable) +473
> System.Diagnostics.EventLog.FindSourceRegistration(String source,
> String machineName, Boolean readOnly)
> System.Diagnostics.EventLog.SourceExists(String source, String
> machineName)
> System.Diagnostics.EventLog.WriteEntry(String message,
> EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
> System.Diagnostics.EventLog.WriteEntry(String message,
> EventLogEntryType type, Int32 eventID, Int16 category)
> System.Diagnostics.EventLog.WriteEntry(String message,
> EventLogEntryType type, Int32 eventID)
> System.Diagnostics.EventLog.WriteEntry(String message,
> EventLogEntryType type)
> System.Diagnostics.EventLog.WriteEntry(String message)



Relevant Pages

  • RE: Microstaff installation chaos!
    ... Event type: Error ... Event source: Serial ... necessary registry information or message DLL files to ... Microsoft Online Partner Support ...
    (microsoft.public.win2000.applications)
  • Re: Registry problem
    ... Event Type: Error ... Computer: STUDENT-ETFEZAF ... Event Source: Application Error ... TraceFileName parameter not located in registry; ...
    (microsoft.public.windowsxp.basics)
  • Re: Explorer restarts
    ... Event Source: Winlogon ... to log out the first account. ... my registry, to my disdain. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: PC Crashed
    ... Event Type: Error ... Event Source: Application Hang ... see Help and Support Center at ... application or service was still using the registry during log off. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Linksys extender crashes
    ... > Event Type: Warning ... see Help and Support Center at ... > Event Source: Media Center Extender Services ... > still using the registry during log off. ...
    (microsoft.public.windows.mediacenter)