With Sharepoint 2007 I am using RunWithElevatedPrivileges
to write to the Event Log, but if I place the call inside the Webpart constructor, I get a "Request Failed" message. However, if I put this in the CreateChildControls
method, it works fine. Within the RunWithElevatedPrivileges
delegate, I am simply creating a new EventLog()
object, nothing more.
I have seen others with the same issue. For example:
https://stackoverflow.com/questions/2387433/trust-set-to-full-but-web-part-still-causes-securityexception
Does anyone know why this is?
[Update:]
This does not work:
public class SSOLogger : System.Web.UI.WebControls.WebParts.WebPart
{
private ILogger logger;
public SSOLogger()
{
EventLog log = new EventLog();
}
protected override void CreateChildControls()
{
}
}
I am getting the following System.Security.SecurityException:
{"Request for the permission of type 'System.Diagnostics.EventLogPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=...' failed."}
The following also does not work:
public class SSOLogger : System.Web.UI.WebControls.WebParts.WebPart
{
private ILogger logger;
public SSOLogger()
{
EventLog log;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
log = new EventLog();
});
}
protected override void CreateChildControls()
{
}
}
This gives me just a
Failed Request.
message in the SecurityException.
The following, however, works:
public class SSOLogger : System.Web.UI.WebControls.WebParts.WebPart
{
private ILogger logger;
public SSOLogger()
{
}
protected override void CreateChildControls()
{
EventLog log = new EventLog();
}
}
I know writing to the event log requires elevated permissions for non-admin accounts, but this does not even work when adding the webpart from a "System Account".