views:

1857

answers:

2

SUMMARY: How to configure a web service such that writing to the Event Log is always possible (regardless of caller)? DETAILS: I have a web service which writes an entry to the Application Log. I established the event source for this by means of a little console application and I think I understand that part of things. When I test this WS, I see I am successfully writing my entry to the Event log.

The virtual directory which hosts this WS does NOT allow anonymous access and is configured for Integrated Windows Auth only.

I have a web client application that calls this Webservice. When the web client site is configured for Integrated Windows Auth only, calls to the Webservice result in logging as desired.

Yet, if I change the web client site to allow anonymous access then the Webservice attempt to log results in an InvalidOperationException. I ignore it but it would be nice to know how to get logging in the webservice regardless of how it is called. Here is a bit of my code:

   public FileService()
    {
        try
        {
            if (!EventLog.SourceExists(g_EventSource))
                EventLog.CreateEventSource(g_EventSource, g_EventLog);

            System.Security.Principal.WindowsIdentity UserIdentityInfo;
            UserIdentityInfo = System.Security.Principal.WindowsIdentity.GetCurrent();
            string AuthType = UserIdentityInfo.AuthenticationType;

 if (AuthType == "Kerberos")
 { engineWSE.Credentials = System.Net.CredentialCache.DefaultCredentials; }
 else
 { engineWSE.Credentials = new System.Net.NetworkCredential("u", "p", "domain"); }

 EventLog.WriteEntry(g_EventSource,
                "Caller: " + UserIdentityInfo.Name +
                " AuthType: " + UserIdentityInfo.AuthenticationType,
                EventLogEntryType.Information, 1);
        }
        catch (InvalidOperationException e)
        {
            // do nothing to ignore: "Cannot open log for source 'myAppSourceName'. You may not have write access." 
        }
    }

The example in the constructor above is sort of contrived for here (I am mainly interested in being able to write out info related to errors in the web service).

I hope there is a way to configure the web service virtual directory (or the code within) so that logging is possible regardless of how it got called.

A: 

Network Service is allowed to write to the Event Log, but not create an event source. you could give permissions to HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\ to allow it to create - but if you've already created it at install time, there's no need.

It's possible that it's failing on the SourceExists as well - since that requires enumerating the same registry key. I'd probably just remove the SourceExists/Create check and trust that it's there - if you're anonymous, you can't create it anyway.

Mark Brackett
+1  A: 

You should also check your web.config.

If IIS is set to anonymous and web.config is set to windows / impersonate. Then it will be the anonymous IIS user that is trying to write to the event log.

Shiraz Bhaiji
Thanks. I re-read my question closely per your answer. I do indeed have <identity impersonate="true"/> and <authentication mode="Windows" /> in the web.config of the website hosting my webservice. I need this because I need the identity of the INTRAnet webclient (see how I test the authentication type and pass credentials accordingly). So, are you saying that because of "impersonate" in the web.config, it is behaving as if I had checked "Anonymous access" for my webservice site? I think you're going to say yes. Given my objectives, is there anyway to configure this for logging in any case?
John Galt
You need to give the IIS anonymous user the right to write to the event log. It's name is IUSR....
Shiraz Bhaiji
I know how to set permissions on files or folders for IUSR_SERVERNAME but how to give that account the right to write to event log?
John Galt
It requires an editing of the registry see: http://blogs.msdn.com/ericfitz/archive/2006/03/01/541462.aspxfor details.
Shiraz Bhaiji
Thanks to you, I found these details below at this link:http://support.microsoft.com/kb/301309Set the following Registry key to 0 instead of 1, and then restart your computer for the changes to take effect.HKLM\System\CurrentControlSet\Services\EventLog\ApplicationName: RestrictGuestAccessType: REG_DWORDNOTE: This enables all Guest accounts to write to the Application Event Log.
John Galt