'System.Security.SecurityException: Requested registry access is not allowed.' when writing to event log

I got this error when I try to create an eventlog source and when I try to write to the new event log (after resorting to manually creating the event log source) with my ASP.NET application.

See the end of this post for the full error message and stack trace.

This seems to be a very commom problem - and not consistent either.  My guess is that it's due to the tightening of security recently, ie XP SP2.  Use of the Network Service account (on win2k3) or AD accounts can help.

To resolve you need to give the ASP.NET user permission to read from the event log registry entries.  And here's how:

  1. Select Start - Run, then enter: regedt32
  2. Navigate/expand to the following key:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Security
  3. Right click on this entry and select Permissions
  4. Add the ASPNET user
  5. Give it Read permission

Bingo! - your app should now work.

HTH

Tim

Full error message:

**

Security Exception

**

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

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

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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) +296

System.Diagnostics.EventLog.SourceExists(String source, String machineName) +79

System.Diagnostics.EventLog.SourceExists(String source) +11

DrKW.Directory.DebugLog.LogEntry(String logMessage, EventLogEntryType type) +16

DrKW.Directory.DebugLog.Error(String logMessage) +18

DrKW.Directory.HttpModule.ConfigSectionHandler.Create(Object parent, Object configContext, XmlNode configNode) in C:\.NETLocalProjects\DrKWDirectory\Directory.HttpModule\Configuration\ConfigSectionHandler.cs:72

System.Web.Configuration.HttpConfigurationRecord.EvaluateRecursive(IConfigurationSectionHandler factory, Object config, String[] keys, Int32 iKey, XmlUtil xml) +333

This article is part of the GWB Archives. Original Author: Tim Huffam

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.