2014-01-17 32 views
1

我想表明一個錯誤給用戶後5失敗的登錄嘗試,然後鎖定了她,我設置在web.config中鎖定用戶之後五次失敗的登錄嘗試,並顯示在MVC3錯誤Asp.net

maxInvalidPasswordAttempts="5" 

現在我想檢查LogOn控制器中的用戶登錄嘗試,並且如果此參數超過5則發送錯誤,我如何計算用戶登錄嘗試並檢查它? 這裏是我的控制器:

// 
    // GET: /Account/LogOn 


    public ActionResult LogOn() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/LogOn 


    [HttpPost] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 

      if (Membership.ValidateUser(model.UserName, model.Password)) 
      { 

        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
         && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
        { 
         return Redirect(returnUrl); 
        } 
        else 
        { 
         return RedirectToAction("Index", "Home"); 
        } 
       } 
       else 
       { 
        ModelState.AddModelError("", "The user name or password provided is incorrect."); 
       } 
     } 

     // If we got this far, something failed, redisplay form 
      return View(model); 
     } 
+0

想要阻止連續登錄?或從一個IP? –

回答

0

試試這個,它可以幫助你

首先創建屬性作爲嘗試和使用JavaScript每次設置與登錄發送。檢查這是否是最大嘗試次數,然後鎖定用戶。

0

在您的其他語句中,您需要堅持當前的失敗嘗試次數,並檢查該次數是否不大於您的設置。

+0

我該怎麼做? 我想擁有當前的失敗嘗試次數 – fahimeh

+0

如上所述,您需要堅持這些信息並進行檢查。這可以通過多種方式完成,包括文本文件,XML,數據庫等。 – ChrisBint

0
  attempt = attempt - 1; 
     MessageBox.Show`enter code here`("Incorrect Credentials"); 
     MessageBox.Show("You only have " + attempt + " attempt(s) left"); 
     con.Close(); 
     } 
    } 
    else 
    { 
     MessageBox.Show("Empty Fields shit"); 
    } 
} 

this.Hide(); 
Form3 ss = new Form3(); 
ss.Show(); 
0

嘗試將它添加爲屬性。

  1. 當登錄失敗設置嘗試在cookie中計數。
  2. 添加自定義屬性。

    public class testAttribute : AuthorizeAttribute 
    { 
        protected override bool AuthorizeCore(HttpContextBase httpContext) 
        { 
    
        HttpCookie cookie = httpContext.Request.Cookies.Get("cookieName"); 
        if(Int32.TryParse(cookie.Value) == 5) 
        { 
        httpContext.Response.StatusCode = 401; 
        httpContext.Response.End(); 
        return false; 
        } 
        else 
        return true; 
        } 
    } 
    
  3. 在您的登錄功能中使用創建的屬性。

    [HttpPost] 
    [testAttribute] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
    if (ModelState.IsValid) 
    { 
    
        if (Membership.ValidateUser(model.UserName, model.Password)) 
        { 
    
          FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
          if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
           && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
          { 
           return Redirect(returnUrl); 
          } 
          else 
          { 
           return RedirectToAction("Index", "Home"); 
          } 
         } 
         else 
         { 
          ModelState.AddModelError("", "The user name or password provided is incorrect."); 
         } 
    } 
    
    // If we got this far, something failed, redisplay form 
        return View(model); 
    } 
    
相關問題