Microsoft KB Archive/329291

= PRB: &quot;Requested Registry Access Is Not Allowed&quot; Error Message When ASP.NET Application Tries to Write New EventSource in the EventLog =

Article ID: 329291

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Internet Information Services 5.0
 * Microsoft Internet Information Services 5.1
 * Microsoft Internet Information Services 6.0

-



This article was previously published under Q329291



IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry



SYMPTOMS
When you use ASP.NET to create a new event source in the event log, you may receive the following error message:

System.Security.SecurityException: Requested registry access is not allowed.



CAUSE
By default, the user token of the ASP.NET worker process is ASPNET (or NetworkService for applications that run on Internet Information Services [IIS] 6.0). The problem in the &quot;Symptoms&quot; section occurs because your account does not have the correct user rights to create an event source.



RESOLUTION
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk. To resolve this problem, a user who has administrative rights must create the event source before you run the ASP.NET Web Application. To create an event source, use one of the following approaches.

First Approach
Create an event source under the Application event log in Registry Editor. To do this, follow these steps:  Click Start, and then click Run. In the Open text box, type regedit . Locate the following registry subkey:

 Right-click the Application subkey, point to New, and then click Key. Type TEST for the key name. Close Registry Editor.</li></ol>

Second Approach
The EventLogInstaller class in the System.Diagnostics namespace permits you to install and configure an event log that your application reads from or writes to while running. You can create an event source by using EventLogInstaller. To do this, follow these steps: <ol> Use Microsoft Visual Basic .NET or Microsoft Visual C# .NET to create a new Class Library named EventLogSourceInstaller. By default, the Class1.vb file or the Class1.cs file is created.</li> In Solution Explorer, right-click EventLogSourceInstaller, and then click Add References.</li> In the Add Reference dialog box, double-click System.Configuration.Install.dll, and then click OK.</li> Rename the Class1.vb\Class1.cs to MyEventLogInstaller.vb\MyEventLogInstaller.cs.</li>  Replace the existing code in MyEventLogInstaller.vb or MyEventLogInstaller.cs with the following sample code:

Visual Basic .NET Sample Imports System.Diagnostics Imports System.Configuration.Install Imports System.ComponentModel

<RunInstaller(True)> _ Public Class MyEventLogInstaller Inherits Installer Private myEventLogInstaller As EventLogInstaller

Public Sub New ' Create an instance of 'EventLogInstaller'. myEventLogInstaller = New EventLogInstaller ' Set the 'Source' of the event log, to be created. myEventLogInstaller.Source = &quot;TEST&quot; ' Set the 'Log' that the source is created in. myEventLogInstaller.Log = &quot;Application&quot; ' Add myEventLogInstaller to 'InstallerCollection'. Installers.Add(myEventLogInstaller) End Sub End Class Visual C# .NET Sample using System; using System.Diagnostics; using System.ComponentModel; using System.Configuration.Install;

namespace EventLogSourceInstaller {   [RunInstaller(true)] public class MyEventLogInstaller : Installer {       private EventLogInstaller myEventLogInstaller;

public MyEventLogInstaller {           //Create Instance of EventLogInstaller myEventLogInstaller = new EventLogInstaller;

// Set the Source of Event Log, to be created. myEventLogInstaller.Source = &quot;TEST&quot;;

// Set the Log that source is created in           myEventLogInstaller.Log = &quot;Application&quot;; // Add myEventLogInstaller to the Installers Collection. Installers.Add(myEventLogInstaller); }   } } </li> On the Build menu, click Build Solution to create EventLogSourceInstaller.dll.</li> Open the Visual Studio .NET Command Prompt.</li> At the command prompt, change to the folder where EventLogSourceInstaller.dll is located.</li> Run the following command to create the EventSource:

InstallUtil EventLogSourceInstaller.dll

</li></ol>

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Use Visual Basic .NET or Visual C# .NET to create a new ASP.NET Web Application. By default, WebForm1.aspx file is created.</li>  In the HTML view of WebForm1.aspx, replace the existing code with the following sample code:

Visual Basic .NET Sample <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;true&quot; %> <%@ Import namespace=&quot;System.Diagnostics&quot; %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <script language=&quot;VB&quot; runat=&quot;server&quot;> Sub WriteEvent_Click(Src As Object, e As EventArgs) Dim ev As New EventLog(&quot;Application&quot;) ' Event's Source name ev.Source = &quot;TEST&quot; EventLog.CreateEventSource(ev.Source, &quot;Application&quot;) Try ev.WriteEntry(TextBox1.Text) Catch b as exception Response.write (&quot;WriteEntry &quot; & b.message & &quot; &quot;) End Try ev = Nothing End Sub

<form id=&quot;Form1&quot; runat=&quot;server&quot;> Event message: <asp:textbox id=&quot;TextBox1&quot; runat=&quot;server&quot; Width=&quot;233px&quot;></asp:textbox> <asp:button id=&quot;Button1&quot; onclick=&quot;WriteEvent_Click&quot; runat=&quot;server&quot; NAME=&quot;Button1&quot; text=&quot;Write to event log&quot;></asp:button> </HTML> Visual C# .NET Sample <%@ Page Language=&quot;c#&quot; AutoEventWireup=&quot;true&quot; %> <%@ Import namespace=&quot;System.Diagnostics&quot; %> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <script language=&quot;C#&quot; runat=&quot;server&quot;> void WriteEvent_Click(Object Src, EventArgs e)   { EventLog ev = new EventLog(&quot;Application&quot;); // Event's Source name ev.Source = &quot;TEST&quot;; EventLog.CreateEventSource(ev.Source, &quot;Application&quot;);

try {               ev.WriteEntry(TextBox1.Text); }           catch (Exception b)            { Response.Write(&quot;WriteEntry &quot; + b.Message + &quot; &quot;); }           ev = null; }

<form id=&quot;Form1&quot; runat=&quot;server&quot;> Event message: <asp:textbox id=&quot;TextBox1&quot; runat=&quot;server&quot; Width=&quot;233px&quot;></asp:textbox> <asp:button id=&quot;Button1&quot; onclick=&quot;WriteEvent_Click&quot; runat=&quot;server&quot; NAME=&quot;Button1&quot; text=&quot;Write to event log&quot;></asp:button> </HTML> </li> On the Debug menu, click Start to view the WebForm1.aspx page in the browser.</li> Type some text in TextBox, and then click Write to event log.</li> The error message that is discussed in the &quot;Symptoms&quot; section of this article appears.</li>  To resolve this problem, create an Event Source as discussed in the &quot;Resolution&quot; section, and comment the following code in WebForm1.aspx : EventLog.CreateEventSource(ev.Source, &quot;Application&quot;) </li> <li>Repeat steps 3 and 4.</li></ol>

<div class="references_section">