我想开发一个windows窗体应用程序来监视我的网络计算机登录、注销和登录尝试详细信息,并根据检测做一些事情。(例如,向管理员发送一些通知)
我试过的是:
我读过关于使用任务计划程序的windows服务、Windows任务计划程序和事件审核的文章,但我想以务实的方式进行。因此,我的问题是如何使用C#以编程方式检测windows登录尝试?
发布于 2016-11-04 12:59:17
若要检测登录尝试,可以依赖windows安全事件。这里您可以看到安全事件的列表及其含义。您可能感兴趣的常见事件是:
4624: An account was successfully logged on.
4625: An account failed to log on.
4648: A logon was attempted using explicit credentials.
4675: SIDs were filtered.
使用应用程序/服务检测事件
您可以通过使用EventLog
类并处理其EntryWritten
事件的代码检测登录尝试。下面的代码示例只是将事件记录在一个文件中,以显示您可以了解并使用该事件。您可以通过电子邮件发送通知、运行应用程序或做其他事情,而不是在文件中写入。
要测试代码,您应该以管理员身份运行。另外,在实际环境中,您应该让它像服务一样,或者在用户登录之前将其配置为运行。
private void Form1_Load(object sender, EventArgs e)
{
EventLog logListener = new EventLog("Security");
logListener.EntryWritten += logListener_EntryWritten;
logListener.EnableRaisingEvents = true;
}
void logListener_EntryWritten(object sender, EntryWrittenEventArgs e)
{
//4624: An account was successfully logged on.
//4625: An account failed to log on.
//4648: A logon was attempted using explicit credentials.
//4675: SIDs were filtered.
var events = new int[] { 4624, 4625, 4648, 4675 };
if (events.Contains(e.Entry.EventID))
System.IO.File.AppendAllLines(@"d:\log.txt", new string[] {
string.Format("{0}:{1}", e.Entry.EventID, e.Entry.Message)
});
}
注意:正如您在问题中所说的,可以在事件发生时使用Windows来做一些事情。
当检测到不成功的登录尝试时,您可以要求Windows为您做一些事情,例如运行应用程序(发送电子邮件或其他东西)。为此,请使用Windows任务计划程序,并在发生特定事件时运行该任务,并指定合适的事件源和Id。另外,要查看完整步骤的示例,请参阅通过电子邮件获取事件日志触发器上的事件日志内容。
https://stackoverflow.com/questions/40423190
复制相似问题