Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net MVC从身份验证弹出窗口捕获用户名_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4_Windows Authentication - Fatal编程技术网

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

C# Asp.net MVC从身份验证弹出窗口捕获用户名,c#,asp.net,asp.net-mvc,asp.net-mvc-4,windows-authentication,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Windows Authentication,我创建的Asp.Net Mvc web应用程序w/c正在使用windows身份验证。 我的要求是捕获并记录无效的登录尝试,但不知道如何做。试图谷歌,但没有运气 列表项 如何从身份验证弹出窗口捕获用户名输入? 列表项 是否有设置限制连续登录失败后的登录弹出窗口。 它在IE浏览器上工作,连续3次登录后显示401未经授权,但Firefox和Mozilla没有限制。 这是我到目前为止试过的。 使用下面的代码 列表项 我试图捕获未经授权的错误,不幸的是,只有在我为Firefox和Mozilla单击取消时才

我创建的Asp.Net Mvc web应用程序w/c正在使用windows身份验证。 我的要求是捕获并记录无效的登录尝试,但不知道如何做。试图谷歌,但没有运气

列表项 如何从身份验证弹出窗口捕获用户名输入? 列表项 是否有设置限制连续登录失败后的登录弹出窗口。 它在IE浏览器上工作,连续3次登录后显示401未经授权,但Firefox和Mozilla没有限制。 这是我到目前为止试过的。 使用下面的代码

列表项 我试图捕获未经授权的错误,不幸的是,只有在我为Firefox和Mozilla单击取消时才会触发事件。 列表项 它在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日志/安全性下的应用程序事件查看器查看。但是我们也可以使用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();

其中jj是我尝试登录页面时键入的用户名,Daniel_2是我的Windows帐户。此值可以通过属性ReplacementStrings轻松提取。在我的例子中,ReplacementStrings[5]让我得到jj。我认为对EventLog条目的查询需要按应用程序和日期时间进行过滤,因此它只在web应用程序部署到IIS中后显示登录

终于成功了,使用Application\u EndRequest事件完全摆脱了我的第一段代码

多亏了德洛普卡特

Global.asax会话_启动事件上的代码

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

}

我认为登录用户应该在HttpContext.Current.Request.ServerVariables[logon\u user]中,但由于登录不成功,所以访问是匿名的。也许这种情况需要另一种方法。derloopkat我尝试了ServerVariables[LOGON\u USER],但仍然为空。感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢回复!感谢,根据正确过滤的事件日志,可以在此时实时阻止它们。创建自己的日志就是重新发明轮子。如果您的要求是显示错误消息,则允许Windows首先验证用户和密码,在成功登录后检查事件日志并阻止用户,将其注销并重定向到错误页面您的帐户被阻止。非常感谢derloopkat,这是一个好主意,我相信这会成功。完成后,我将发布我的最终代码。
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);

}