C# EventLog.Delete Access Denied

Viewed 691

I have read other questions on SO in regards to security and registry keys, nothing has helped me solve my particular use case scenario.

Here's my scenario:

What I'm Trying To Do

I want to, in code, delete a windows event log.

The Problem

When executing the function, I receive a System.ComponentModel.Win32Exception. The exception message is "Access is denied".

How I Am Doing It Currently

I am using an impersonator function that I wrote which wraps around the EventLog.Delete function, it drops me into a user context that has full access to the EventLog Registry Hive. Subsequently the logs I am interested in also have full access for this particular user.

My Question

Why do I receive a "Access Is Denied" if the user I am running under (through impersonation) has full access to the log in question? I've tested my Impersonation function and it works as expected for other code I've written. I don't get why I would get access denied for this.

In another scenario with my impersonation function it works just fine, for example if I tried to write to a file that the user context that is running the program does not have write access to, then I would not be able to write to the text file, however if I use my impersonation to drop into a user context that does have write access then it works just fine (I can write to the file). So I just don't understand why the same concept can't be applied to registry keys.

What am I missing here?

The Code

  1. Exception Message Access Denied exception Deleting EventLog

  2. My Test

Where sw-test is a user I created for testing purposes, it has full access permissions to the registry we are trying to delete.

        [TestMethod]
        public void DeleteEventLog_ValidatedUser_DeleteLog()
        {
            using (new Impersonator(Environment.UserDomainName, "sw-test", "pswd"))
            {
                Logging logging = new Logging();
                logging.DeleteEventLog("testLog");
            }
        }
1 Answers
Related