如何让EventLog将用户名记录到Window事件日志中?

Mat*_*tin 5 c# event-log

我正在使用C#写入Windows事件日志.我可以在mmc.exe"计算机管理"工具中设置每个字段,但"用户"字段除外.

客户端应用程序是ASP.NET并使用表单身份验证.

public static void WriteOnce()
{
    EventLog log = new EventLog("MyApp");
    if (!EventLog.SourceExists("MySource"))
    {
        EventSourceCreationData data = new EventSourceCreationData("MySource", "MyApp");
        EventLog.CreateEventSource(data);
    }
    log.Source = "MySource";
    log.WriteEntry("Hello World", EventLogEntryType.Information,123,456,new byte[]{1,2,3});
}
Run Code Online (Sandbox Code Playgroud)

更新:我检查,在ASP.NET中,即使设置身份impersonation = true&authentication = windows仍然没有用户.

我还在控制台应用程序中检查过没有用户.

Mat*_*tin 0

我找到了一篇博客文章,解释了如何做到这一点,尽管似乎没有完全托管的方法来做到这一点。要捕获用户 ID,您必须使用 pinvoke/native 方法调用。

http://www.infosysblogs.com/microsoft/2007/09/logging_events_with_user_detai_1.html

对于上述情况,它将用户记录为 ASPNET 或网络服务,或控制台应用程序的登录用户。api 调用本身采用一个指向 SID 的指针参数。我没有尝试看看是否可以进行欺骗。

JPucket 可能是对的,在系统事件日志中获取表单验证用户 ID 的唯一方法是通过消息字段。