【问题标题】:Eventlog, impersonation and Access Denied Win32Exception事件日志、模拟和拒绝访问 Win32Exception
【发布时间】:2009-08-04 06:19:28
【问题描述】:

我正在使用专用帐户(使用 SDDL 策略)将事件日志条目写入自定义事件日志。为此,我使用 WindowsImpersonationContext 并使用 LogonUser 获取令牌:

WindowsIdentity impersonationIdentity = new WindowsIdentity(ptr);            
WindowsImpersonationContext impersonationContext = impersonationIdentity.Impersonate();
EventLog.WriteEntry("MyCustomSource", DateTime.Now.ToLongTimeString(), EventLogEntryType.Warning);
impersonationContext.Undo();
NativeMethods.CloseHandle(ptr);

这段代码产生了事件日志条目,但我也得到了一个 Win32Exception:

Unhandled Exception: System.InvalidOperationException: Cannot open log for source 'MyCustomSource'. You may not have write access. ---> System.ComponentModel.Win32Exception: Access is denied

现在,如果我在模拟行之后放置一个 Thread.Sleep(500),异常就会消失:

WindowsImpersonationContext impersonationContext = impersonationIdentity.Impersonate();
System.Threading.Thread.Sleep(500);

导致此异常的原因是什么?即使出现拒绝访问异常,事件日志条目又是如何被写入的?

编辑: 在使用之前,我已经使用相关日志注册了事件源。我只包含了小代码 sn-ps 以保持消息简短。

【问题讨论】:

    标签: c# impersonation event-log win32exception


    【解决方案1】:

    这个问题很老,似乎没有答案。 看起来像是我的问题 (https://stackoverflow.com/questions/17997152/registereventsource-fails-with-access-denied-for-impersonated-user-in-asp-net)。

    我将此问题链接到我的问题,因为我认为它是同一个问题,并且我进一步调查了它。它与 LogonUser() 调用中指定的 logonType 有关。

    也许有帮助。

    【讨论】:

      猜你喜欢
      • 2021-09-04
      • 1970-01-01
      • 2014-10-04
      • 2017-09-15
      • 1970-01-01
      • 2016-08-15
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多