1

Windows認証を使用しているAsp.Net Mvc Webアプリケーションを作成しました。 私の要件は、ログインの無効なログインをキャプチャしてログに記録する方法ですが、その方法はわかりません。 googleにしようとしたが運がない。認証ポップアップからユーザ名の入力をキャプチャする方法Asp.net MVC認証ポップアップからユーザー名を取得

  1. リスト項目 ?
  2. リスト項目 ログイン失敗後にログインを制限する設定はありますか? これはInternet Explorer(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 "); 
        } 
    } 
    

おかげで、誰かが助けることができると思います。

+0

私は、ログオンユーザーが 'HttpContext.Current.Requestであるべきと考えています。ServerVariables ["LOGON_USER"] 'しかし、ログインが成功しなかったので、アクセスは匿名です。おそらく、この状況には別のアプローチが必要です。 – derloopkat

+0

derloopkat ServerVariables ["LOGON_USER"]を試しましたが、まだ空です。返信ありがとう – windstoner

答えて

0

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アプリケーションへのログオンしか表示されません。

+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); 
    
    } 
    
関連する問題