如何使一个.NET Windows服务检测登录,注销和切换用户的事件? [英] How to make a .NET Windows Service detect Logon, Logoff and Switch User events?

查看:2256
本文介绍了如何使一个.NET Windows服务检测登录,注销和切换用户的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要跟踪用户当前的Windows XP SP3(使用控制台中的一个)

I need to track the current in user (the one using the console) on Windows XP SP3.

我试过如下:


  • Microsoft.Win32.SystemEvents.SessionSwitch:Works的单点登录/注销事件,但无法检测切换用户

  • Microsoft.Win32.SystemEvents.SessionSwitch: Works for single logon/logout events, but fails to detect switch user.

如果出现以下情况:


  1. 用户A登录

  2. 用户A切换用户

  3. 用户b登录

  4. 用户b注销

  5. 用户A的恢复会话

  1. userA log in
  2. userA switch user
  3. userB login
  4. userB logout
  5. userA restore session

事件3和4没有被检测到SystemEvents.SessionSwitch

Events 3 and 4 are not detected by SystemEvents.SessionSwitch

监视安全事件日志:活动是不一致的,按顺序到达。例如,如果上面的列表重播,我收到用户A的事件ID 528(登录),后面两个538(注销),他恢复他的会议结束后。检查event.TimeGenerated于事无补。此方法还,如果审核上SecPol.msc禁用不起作用

Monitoring the "Security" EventLog: Events are inconsistent and arrive out of order. For instance, if the list above is replayed, I receive an event id 528 (Logon), followed by two 538 (Logoff) for userA after he restores his session. Checking event.TimeGenerated doesn't help. This method also does not work if auditing is disabled on SecPol.msc.

P / WTSRegisterSessionNotification中调用:正​​常工作。我不得不创建一个隐藏的表单,覆盖其的WndProc处理WM_WTSSESSION_CHANGE的消息,然后调用WTSQuerySessionInformation来获取与事件相关的用户名。这种方法看起来太复杂了,有没有简单的方法

P/Invoking WTSRegisterSessionNotification: Works fine. I had to create a hidden form, override its WndProc to handle WM_WTSSESSION_CHANGE messages, and then call WTSQuerySessionInformation to get the username associated with the event. This method looks too complex, is there a simpler way?

编辑:


  • 电话WTSGetActiveConsoleSessionId每n毫秒的作品太多,但我正在寻找一个基于事件的方法。

推荐答案

如果你正在做一个服务,您的类是从 ServiceBase 。考虑调查 OnSessionChange 方法。重写此方法来检测不同类型的会议上改变。它提供了用于会话变化的原因,并在新的会话标识符。请务必在构造函数来设置 CanHandleSessionChangeEvent 真正,否则你的倍率不会被调用。

If you're making a service, your class is derived from ServiceBase. Consider investigating the OnSessionChange method. Override this method to detect different types of session changes. It provides both a reason for the session change, and the new session identifier. Be sure in your constructor to set CanHandleSessionChangeEvent to true, otherwise your override will not be called.

这篇关于如何使一个.NET Windows服务检测登录,注销和切换用户的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆