[英]Asp.net MVC Capture user name from authentication popup
我创建的Asp.Net Mvc Web应用程序使用Windows身份验证。 我的要求是捕获并记录无效的登录尝试,但不知道该怎么做。 尝试谷歌,但没有运气。
到目前为止,这是我尝试过的。 使用以下代码,
列表项在IE中3次无效尝试后将触发,但不知道如何获取用户名输入。 Global.asax
protected void Application_EndRequest(Object sender, EventArgs e) { HttpContext context = HttpContext.Current; if (context.Response.Status.Substring(0, 3).Equals("401")) { //Capture user name for further processing // context.Response.ClearContent(); context.Response.Write("You are un authorized "); } }
在此先感谢您,希望有人可以提供帮助。
Windows已经在Windows事件日志中捕获并记录了无效的登录尝试。 可以使用Windows Logs/Security
下的应用程序“ Event Viewer
看到。 但是我们也可以使用C#检索这些日志。
以管理员身份打开Visual Studio并添加此代码。 仅出于测试目的,我们将获得最后10条记录。
EventLog securityLog = new EventLog("Security");
var logOnAttempts = (from log in securityLog.Entries.Cast<EventLogEntry>()
where log.EntryType==EventLogEntryType.SuccessAudit
orderby log.TimeGenerated descending
select log
).Take(10).ToList();
我上次日志的属性Message
说:
A logon was attempted using explicit credentials.
Subject:
Security ID: S-1-5-21-3657345512-3965846940-1053971979-1002
Account Name: Daniel_2
Account Domain: Acer
Logon ID: 0x29058
Account Whose Credentials Were Used:
Account Name: jjjjj
Account Domain:
其中“ jjjjj”是我尝试登录页面时键入的用户名,而Daniel_2是我的Windows帐户。 通过属性ReplacementStrings
可以轻松提取此值。 在我的情况下, ReplacementStrings[5]
让我“ jjjjj”。 我认为对EventLog条目的查询需要按应用程序和日期时间进行过滤,因此,一旦将其部署在IIS中,它只会显示登录到您的Web应用程序。
终于使它工作了,使用Application_EndRequest事件完全摆脱了我的第一个代码。
感谢derloopkat。
Global.asax Session_Start事件上的代码。
protected void Session_Start(object sender, EventArgs e) { if (HttpContext.Current.User.Identity.IsAuthenticated) { string currentUser = HttpContext.Current.User.Identity.Name; Int32 expiryMin = Convert.ToInt32(ConfigurationManager.AppSettings["CacheExpirationInMinutes"]); // call our procedure auditLog(currentUser); bool IsActive = accessMaintenance.IsActive(currentUser); if (IsActive) { // handling if user is valid/not locked... } else { // Other handling if user is locked... } } }
auditLog程序
private void auditLog(string user) { // Get logs from event viewer string userName = ExtractUserAlias(user); EventLog securityLog = new EventLog("Security"); var logOnAttempts = ( from log in securityLog.Entries.Cast<EventLogEntry>() where log.EventID == 4625 || log.EventID== 4624 && log.ReplacementStrings[5] == userName orderby log.TimeGenerated descending select log ).Take(20).ToList(); //Store user logs to db if logs does not exists. //Store in DB for reporting purposes DataAccess db = new DataAccess(); foreach (var x in logOnAttempts) { string entryType = ""; switch (x.EntryType) { case EventLogEntryType.SuccessAudit: entryType = "SuccessAudit"; break; case EventLogEntryType.FailureAudit: entryType = "FailureAudit"; break; } SqlCommand com = new SqlCommand(); com.CommandType = System.Data.CommandType.StoredProcedure; com.CommandText = "Sp_LogUser"; com.Parameters.AddWithValue("@UserName", userName); com.Parameters.AddWithValue("@EntryType", entryType); com.Parameters.AddWithValue("@TimeGenerated", x.TimeGenerated); com.Parameters.AddWithValue("@Details", x.Message); db.ExecuteNonQuery(com); } // logic to to validate and lock user SqlCommand com2 = new SqlCommand(); com2.CommandType = System.Data.CommandType.StoredProcedure; com2.CommandText = "Sp_validateAndLockUser"; com2.Parameters.AddWithValue("@Username", @userName); db.ExecuteNonQuery(com2); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.