1

Windows 인증을 사용하여 Asp.Net MVC 웹 응용 프로그램을 만들었습니다. 나의 요구 사항은 잘못된 로그인 시도를 캡처하고 로그하지만이를 수행하는 방법을 모른다. google 그러나 운 없음을 시험해 보았다.Asp.net MVC 인증 팝업에서 사용자 이름을 캡처

  1. 목록 항목 인증 팝업에서 사용자 이름 입력을 캡처하는 방법은 무엇입니까?
  2. 목록 항목 연속 로그인 실패 후 로그인 팝업을 제한하는 설정이 있습니까? Internet Explorer (IE)에서 작동하며 3 회 연속 로그인 시도 후에 401 인증을 표시하지만 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 "); 
        } 
    } 
    

덕분에, 누군가가 도움을 줄 수 있기를 바랍니다.

+0

나는 로그온 사용자가 HttpContext.Current.Request'에 있어야합니다 생각합니다.ServerVariables [ "LOGON_USER"]'그러나 로그인에 실패 했으므로 액세스는 익명입니다. 아마도이 상황은 다른 접근 방식을 필요로합니다. – derloopkat

+0

derloopkat ServerVariables [ "LOGON_USER"]를 사용해 보았지만 여전히 비어 있습니다. 회신을 보내 주셔서 감사합니다. – windstoner

답변

0

Windows는 이미 Windows Event Log에서 잘못된 로그온 시도를 캡처하고 기록합니다. 응용 프로그램 Event Viewer을 사용하여 Windows Logs/Security에서 확인할 수 있습니다. 그러나 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는 말한다 :

"jjjjj는"페이지에 로그인을 시도하고 Daniel_2 내 Windows 계정 때 내가 입력 한 사용자 이름입니다
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:  

. 이 값은 속성 ReplacementStrings을 통해 쉽게 추출 할 수 있습니다. 내 경우에는 ReplacementStrings[5] 나를 "jjjjj"가져옵니다. EventLog 항목에 대한 쿼리는 응용 프로그램과 날짜별로 필터링해야하므로 IIS에 배포 된 웹 응용 프로그램에 대한 로그온 만 표시됩니다.

+0

정말로 도움이되는 정보 주셔서 감사합니다. 로그를 추출 할 수는 있지만 로그인 할 때 실제 사용자 이름이 필요합니다. 내 요구 사항은 연속 된 잘못된 시도를 실시간으로 사용 중지하기 때문입니다. – windstoner

+0

'Application_EndRequest()'에서 "무단 액세스"HTTP 401을 탐지하면 제대로 필터링 된 이벤트 로그에 따라 실시간으로 차단 될 수 있습니다. 자신 만의 로그를 만드는 것은 바퀴를 재발 명하는 것입니다. 요구 사항에 오류 메시지가 표시되면 Windows에서 사용자와 암호의 유효성을 검사하고 이벤트 로그를 성공적으로 확인한 후 사용자를 차단 한 다음 로그 아웃하고 오류 페이지 "사용자의 계정이 차단되었습니다"로 리디렉션하도록하십시오. – derloopkat

+0

덕분에 너무 많은 derloopkat, 좋은 아이디어와 나는 이것이 트릭을 할 것이라고 믿습니다. 한 번 완료되면 최종 코드를 게시합니다. – windstoner

0

마지막으로 Application_EndRequest 이벤트를 사용하여 첫 번째 코드를 제거합니다.

derloopkat에게 감사드립니다.

  • 코드 on 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); 
    
    }