Asp.net MVC 从身份验证弹出窗口中捕获用户名

Asp.net MVC Capture user name from authentication popup

我创建了 Asp.Net Mvc web 应用程序 w/c 正在使用 windows 身份验证。 我的要求是捕获并记录无效的登录尝试,但不知道该怎么做。尝试 google 但没有成功。

  1. 列表项 如何从身份验证弹出窗口中捕获用户名输入?
  2. 列表项 是否有设置限制连续登录失败后弹出登录。 它适用于 Internet Explorer (IE),连续 3 次尝试登录后显示 401 unauthorized 但 Firefox 和 Mozilla 没有限制。

这是我到目前为止尝试过的方法。 使用下面的代码,

在此先感谢,希望有人能提供帮助。

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 条目的查询需要按应用程序和日期时间进行过滤,因此它只会在您的 Web 应用程序部署到 IIS 中后显示登录信息。

终于成功了,完全摆脱了我使用 Application_EndRequest 事件的第一个代码。

感谢 derloopkat。

  • Global.asaxSession_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...
    
            }
    
        }
    }
    
  • 审计日志程序

    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);
    
    }