C# 如何跟踪窗口';用户活动?

C# 如何跟踪窗口';用户活动?,c#,winforms,events,authentication,logoff,C#,Winforms,Events,Authentication,Logoff,我需要跟踪与审核策略类似的用户活动。 如果可能,我想使用我自己的程序跟踪windows用户的活动。先谢谢你。。。 我正在使用以下代码 using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using System.Configuration; using System.IO; using COMAdmin; namespace Console

我需要跟踪与审核策略类似的用户活动。 如果可能,我想使用我自己的程序跟踪windows用户的活动。先谢谢你。。。 我正在使用以下代码

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Configuration;
using System.IO;
using COMAdmin;
namespace ConsoleApplication3
{
    class Program
    {
        static SensEvents SensEvents = new SensEvents();

        static void Main(string[] args)
        {
            SensEvents.LogonEvent += OnSensLogonEvent;
            Console.WriteLine("Waiting for events. Press [ENTER] to stop.");
            Console.ReadLine();
        }

        static void OnSensLogonEvent(object sender, SensLogonEventArgs e)
        {
            String date = DateTime.Now.ToString("M/d/yyyy hh:mm:ss tt");
            Console.WriteLine("Type:" + e.Type + ", UserName:" + e.UserName + ", SessionId:" + e.SessionId + ", Date :" + date);
        }
    }

    public sealed class SensEvents
    {
        private static readonly Guid SENSGUID_EVENTCLASS_LOGON2 = new Guid("d5978650-5b9f-11d1-8dd2-00aa004abd5e");
        private Sink _sink;

        public event EventHandler<SensLogonEventArgs> LogonEvent;

        public SensEvents()
        {
            _sink = new Sink(this);
            COMAdminCatalogClass catalog = new COMAdminCatalogClass(); 

            ICatalogCollection subscriptions = (ICatalogCollection)catalog.GetCollection("TransientSubscriptions");

            ICatalogObject subscription = (ICatalogObject)subscriptions.Add();
            subscription.set_Value("EventCLSID", SENSGUID_EVENTCLASS_LOGON2.ToString("B"));
            subscription.set_Value("SubscriberInterface", _sink);
            // NOTE: we don't specify a method name, so all methods may be called
            subscriptions.SaveChanges();
        }

        private void OnLogonEvent(SensLogonEventType type, string bstrUserName, uint dwSessionId)
        {
            EventHandler<SensLogonEventArgs> handler = LogonEvent;
            if (handler != null)
            {
                handler(this, new SensLogonEventArgs(type, bstrUserName, dwSessionId));
            }
        }

        private class Sink : ISensLogon2
        {
            private SensEvents _events;

            public Sink(SensEvents events)
            {
                _events = events;
            }

            public void Logon(string bstrUserName, uint dwSessionId)
            {
                _events.OnLogonEvent(SensLogonEventType.Logon, bstrUserName, dwSessionId);
            }

            public void Logoff(string bstrUserName, uint dwSessionId)
            {
                _events.OnLogonEvent(SensLogonEventType.Logoff, bstrUserName, dwSessionId);
            }

            public void SessionDisconnect(string bstrUserName, uint dwSessionId)
            {
                _events.OnLogonEvent(SensLogonEventType.SessionDisconnect, bstrUserName, dwSessionId);
            }

            public void SessionReconnect(string bstrUserName, uint dwSessionId)
            {
                _events.OnLogonEvent(SensLogonEventType.SessionReconnect, bstrUserName, dwSessionId);
            }

            public void PostShell(string bstrUserName, uint dwSessionId)
            {
                _events.OnLogonEvent(SensLogonEventType.PostShell, bstrUserName, dwSessionId);
            }
        }

        [ComImport, Guid("D597BAB4-5B9F-11D1-8DD2-00AA004ABD5E")]
        private interface ISensLogon2
        {
            void Logon([MarshalAs(UnmanagedType.BStr)] string bstrUserName, uint dwSessionId);
            void Logoff([In, MarshalAs(UnmanagedType.BStr)] string bstrUserName, uint dwSessionId);
            void SessionDisconnect([In, MarshalAs(UnmanagedType.BStr)] string bstrUserName, uint dwSessionId);
            void SessionReconnect([In, MarshalAs(UnmanagedType.BStr)] string bstrUserName, uint dwSessionId);
            void PostShell([In, MarshalAs(UnmanagedType.BStr)] string bstrUserName, uint dwSessionId);
        }
    }

    public class SensLogonEventArgs : EventArgs
    {

        public SensLogonEventArgs(SensLogonEventType type, string userName, uint sessionId)
        {
            Type = type;
            UserName = userName;
            SessionId = sessionId;
        }
        private String userName;
        public string UserName {
            get { return userName; }
            set { userName = value; }
           }
        private uint sessionId;
        public  uint SessionId {

            get { return sessionId; }
            set { sessionId = value; }
        }
        private SensLogonEventType type;
        public  SensLogonEventType Type {
            get { return type; }
            set { type = value; }
            //get; private set; 
        }
    }

    public enum SensLogonEventType
    {
        Logon,
        Logoff,
        SessionDisconnect,
        SessionReconnect,
        PostShell
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Runtime.InteropServices;
使用系统文本;
使用系统配置;
使用System.IO;
使用COMAdmin;
命名空间控制台应用程序3
{
班级计划
{
静态SensEvents SensEvents=新SensEvents();
静态void Main(字符串[]参数)
{
SensEvents.LogonEvent+=OnSensLogonEvent;
Console.WriteLine(“等待事件。按[ENTER]停止”);
Console.ReadLine();
}
静态void OnSensLogonEvent(对象发送方,SensLogonEventArgs e)
{
字符串date=DateTime.Now.ToString(“M/d/yyyy hh:mm:ss tt”);
Console.WriteLine(“类型:+e.Type+”,用户名:+e.UserName+”,会话ID:+e.SessionId+”,日期:+Date);
}
}
公开密封类活动
{
专用静态只读Guid SENSGUID_EVENTCLASS_LOGON2=新Guid(“d5978650-5b9f-11d1-8dd2-00aa004abd5e”);
私人水槽;;
公共事件处理程序LogonEvent;
公众活动()
{
_水槽=新水槽(本);
COMAdminCatalogClass catalog=新的COMAdminCatalogClass();
ICatalogCollection订阅=(ICatalogCollection)catalog.GetCollection(“TransientSubscriptions”);
ICatalogObject订阅=(ICatalogObject)订阅。添加();
subscription.set_值(“EventCLSID”,SENSGUID_EVENTCLASS_LOGON2.ToString(“B”);
subscription.set_值(“subscriptberinterface”,_sink);
//注意:我们没有指定方法名,因此可以调用所有方法
subscriptions.SaveChanges();
}
私有void OnLogonEvent(SensLogonEventType、字符串bstrUserName、uint dwSessionId)
{
EventHandler=LogonEvent;
if(处理程序!=null)
{
handler(这是新的SensLogonEventArgs(type,bstrUserName,dwSessionId));
}
}
私有类接收器:ISensLogon2
{
私人事件(SensEvents);;
公共接收器(传感器事件)
{
_事件=事件;
}
公共无效登录(字符串bstrUserName,uint dwSessionId)
{
_events.OnLogonEvent(SensLogonEventType.Logon、bstrUserName、dwSessionId);
}
公共作废注销(字符串bstrUserName,uint dwSessionId)
{
_events.OnLogonEvent(SensLogonEventType.Logoff、bstrUserName、dwSessionId);
}
public void SessionDisconnect(字符串bstrUserName,uint dwSessionId)
{
_events.OnLogonEvent(SensLogonEventType.SessionDisconnect、bstrUserName、dwSessionId);
}
public void SessionReconnect(字符串bstrUserName,uint dwSessionId)
{
_events.OnLogonEvent(SensLogonEventType.SessionReconnect、bstrUserName、dwSessionId);
}
public void PostShell(字符串bstrUserName,uint dwSessionId)
{
_events.OnLogonEvent(SensLogonEventType.PostShell、bstrUserName、dwSessionId);
}
}
[ComImport,Guid(“D597BAB4-5B9F-11D1-8DD2-00AA004ABD5E”)]
专用接口ISensLogon2
{
无效登录([Marshallas(UnmanagedType.BStr)]字符串bstrUserName,uint dwSessionId);
无效注销([In,Marshallas(UnmanagedType.BStr)]字符串bstrUserName,uint dwSessionId);
void SessionDisconnect([In,marshallas(UnmanagedType.BStr)]字符串bstrUserName,uint dwSessionId);
void SessionReconnect([In,marshallas(UnmanagedType.BStr)]字符串bstrUserName,uint dwSessionId);
void PostShell([In,marshallas(UnmanagedType.BStr)]字符串bstrUserName,uint dwSessionId);
}
}
公共类SensLogonEventArgs:EventArgs
{
公共SensLogonNeventargs(SensLogonNeventType类型、字符串用户名、uint会话ID)
{
类型=类型;
用户名=用户名;
SessionId=SessionId;
}
私有字符串用户名;
公共字符串用户名{
获取{返回用户名;}
设置{userName=value;}
}
非公开uint会话ID;
公共uint会话ID{
获取{return sessionId;}
设置{sessionId=value;}
}
私有SensLogonNeventType类型;
公共SensLogonEventType类型{
获取{返回类型;}
设置{type=value;}
//获得;私人设置;
}
}
公共枚举SensLogonEventType
{
登录,
注销,
会话断开连接,
会话重新连接,
后壳
}
}

您可以打开内置的审核功能并读取审核日志。这比尝试复制相同的功能要容易得多。

您可以打开内置的审核功能并读取审核日志。这比尝试复制相同的功能要容易得多。

您知道,我可能会删除您对任何人的敬意。不需要它们。通过投票和接受感谢用户。正如你们所知,我可能会删除你们对任何人的赞扬。不需要它们。通过投票和接受感谢用户。如果启用内置审核功能,则感谢响应跟踪太多事件可能会导致系统严重减速。更重要的是,由于并非所有的用户活动都对日志记录感兴趣,所以审核策略使我们只捕获那些我们认为重要的事件类型。您可以只审核特定事件,或者访问特定资源。