简体   繁体   English

OnSessionChange有时不起作用

[英]OnSessionChange does not work sometimes

I have a windows service that should check log in and log out of all users and insert them all in a table.I have used OnSessionChange() and OnShutDown() events in my service to do that.It sometimes works fine but sometimes nothing happen when a user logging in or shutting down without any error,i think sometimes those events do not fire. 我有一个Windows服务,应该检查所有用户的登录和注销并将它们全部插入到表中。我在服务中使用OnSessionChange()OnShutDown()事件来做到这一点,虽然有时效果很好,但有时什么也没发生当用户登录或关闭而没有任何错误时,我认为有时这些事件不会触发。

I do not know what is my mistake?? 我不知道我的错误是什么?

EDIT : 编辑:

I want to create a winservice to save log in and log out time of all accounts in a domain. 我想创建一个winservice来保存域中所有帐户的登录和注销时间。 I have tried OnSessionChange() and check out the SessionChangeDescription property but it sometimes does not work correctly, so i decided to use api functions to do that. 我已经尝试过OnSessionChange()并检查出SessionChangeDescription属性,但是有时它无法正常工作,所以我决定使用api函数来做到这一点。 In order to my searches LsaGetLogonSessionData and maybe ISensLogon2 functions can return what i want. 为了我的搜索LsaGetLogonSessionData和ISensLogon2函数可以返回我想要的。 but the question is that where should i use this function? 但问题是我应该在哪里使用此功能? Should i call this function in an event or it can diagnose the log ins and log outs itself? 我应该在事件中调用此功能,还是可以诊断登录并自行注销?

I'm waiting yet for your replies.... Thanks in advance... 我正在等待您的回复。

Maybe your service wasn't started before the fired event when logging in. But Log-out should works for those reason. 登录时,可能未在触发事件之前启动您的服务。但出于这些原因,注销应该可以进行。

You should check first that "CanHandleSessionChangeEvent" and "CanHandlePowerEvent" are True. 您应该首先检查“ CanHandleSessionChangeEvent”和“ CanHandlePowerEvent”是否为True。

for those who has problem with fetch username from SessionChangeDescription.SessionId: 对于从SessionChangeDescription.SessionId获取用户名有问题的用户:

sometimes, in SessionLogoff when you need access user that logged off,log off action did fast and SessionId lost. 有时,在SessionLogoff中,当您需要注销的访问用户时,注销操作执行得很快而SessionId丢失了。

my solution was this: 我的解决方案是这样的:

1.define a global array(like Dictionary<int, string> userlist ) 1.定义一个全局数组(例如Dictionary<int, string> userlist

2.store session id and usernames in SessionLogon event(like userlist.Add(SessionChangeDescription.SessionId,GetUsernameBySessionId(SessionChangeDescription.SessionId) ) 2.在SessionLogon事件中存储会话ID和用户名(如userlist.Add(SessionChangeDescription.SessionId,GetUsernameBySessionId(SessionChangeDescription.SessionId) ))

3.access username in every where by array(like userlist[changeDescription.SessionId] ) 3.通过数组在每个位置访问用户名(如userlist[changeDescription.SessionId]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM