思路:
1,通用配置(錯誤次數與間隔時間)可以修改,不需要發布代碼
2,用戶登錄錯誤次數>=設定的錯誤次數,進行判斷在時間內不能累加錯誤次數,彈出提示
3,間隔時間外錯誤次數清0
4,用戶名不存在,記錄IP判斷次數(走一次用戶名存在密碼錯誤的過程,不要直接加黑ip ,可能有情況的)
5, 用戶登錄密碼錯誤時 錯誤次數累加 與 時間記錄
6,登錄成功,錯誤次數清0
上代碼
public UserEntity CheckLogin(string username, string password) { UserEntity userEntity = service.CheckLogin(username); var errorCount = Config.GetValue("ErrorCount").ToInt(); var errorTime = Config.GetValue("ErrorTime").ToInt(); if (userEntity != null && userEntity.OrganizeId != "") { if (userEntity.EnabledMark == 1) { if (userEntity.ErrorCount != null && userEntity.ErrorCount >= errorCount) { DateTime errortime = Convert.ToDateTime(userEntity.ErrorTime); //Subtract函數減去指定時間,返回一個時間差,時間的格式可以是分鍾也可以是秒、小時 TimeSpan span = DateTime.Now.Subtract(errortime); double minute = span.TotalMinutes;//取時間間隔的分鍾數 if (minute < errorTime) { throw new Exception("您已經連續" + errorCount + "次輸入密碼錯誤,請" + errorTime + "分鍾之后再次重試!"); } else { userEntity.ErrorCount = 0; service.SaveForm(userEntity.UserId, userEntity); } } string dbPassword = Md5Helper.MD5(DESEncrypt.Encrypt(password.ToLower(), userEntity.Secretkey).ToLower(), 32).ToLower(); if (dbPassword == userEntity.Password) { //登錄成功后,錯誤次數清0 userEntity.ErrorCount = 0; DateTime LastVisit = DateTime.Now; int LogOnCount = (userEntity.LogOnCount).ToInt() + 1; if (userEntity.LastVisit != null) { userEntity.PreviousVisit = userEntity.LastVisit.ToDate(); } userEntity.LastVisit = LastVisit; userEntity.LogOnCount = LogOnCount; userEntity.UserOnLine = 1; service.UpdateEntity(userEntity); return userEntity; } else { userEntity.ErrorCount = (userEntity.ErrorCount==null?0:userEntity.ErrorCount).ToInt() + 1; userEntity.ErrorTime = System.DateTime.Now; service.UpdateEntity(userEntity); throw new Exception("密碼和賬戶名不匹配!"); } } else { throw new Exception("賬戶名被系統鎖定,請聯系管理員!"); } } else { //判斷客戶端IP限制 FilterIPEntity filterIPEntity = filterService.CheckErrorIp(Net.Ip); if(filterIPEntity!=null) { if (filterIPEntity.ErrorCount!=null&&filterIPEntity.ErrorCount >= errorCount) { DateTime errortime =Convert.ToDateTime(filterIPEntity.ErrorTime); //Subtract函數減去指定時間,返回一個時間差,時間的格式可以是分鍾也可以是秒、小時 TimeSpan span = DateTime.Now.Subtract(errortime); double minute = span.TotalMinutes;//取時間間隔的分鍾數 if (minute < errorTime) { throw new Exception("您已經連續"+errorCount+"次輸入賬號密碼錯誤,請"+errorTime+"分鍾之后再次重試!"); } else { filterIPEntity.ErrorCount = 0; filterService.SaveForm(filterIPEntity.FilterIPId, filterIPEntity); } } else { filterIPEntity.ErrorCount = (filterIPEntity.ErrorCount == null ? 0 : filterIPEntity.ErrorCount).ToInt() + 1; filterIPEntity.ErrorTime = System.DateTime.Now; filterService.SaveForm(filterIPEntity.FilterIPId, filterIPEntity); } } else { filterIPEntity = new FilterIPEntity(); filterIPEntity.ErrorCount = (filterIPEntity.ErrorCount==null?0:filterIPEntity.ErrorCount).ToInt() + 1; filterIPEntity.ErrorTime = System.DateTime.Now; filterIPEntity.ErrorIp = Net.Ip; filterService.SaveForm("", filterIPEntity); } throw new Exception("賬戶名或密碼錯誤,請重新輸入!"); } }
代碼有些冗余的,自行處理
獲取IP代碼
/// <summary> /// 獲取Ip /// </summary> public static string Ip { get { var result = string.Empty; if (HttpContext.Current != null) result = GetWebClientIp(); if (result.IsEmpty()) result = GetLanIp(); return result; } } /// <summary> /// 獲取Web客戶端的Ip /// </summary> private static string GetWebClientIp() { var ip = GetWebRemoteIp(); foreach (var hostAddress in Dns.GetHostAddresses(ip)) { if (hostAddress.AddressFamily == AddressFamily.InterNetwork) return hostAddress.ToString(); } return string.Empty; } /// <summary> /// 獲取Web遠程Ip /// </summary> private static string GetWebRemoteIp() { return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } /// <summary> /// 獲取局域網IP /// </summary> private static string GetLanIp() { foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName())) { if (hostAddress.AddressFamily == AddressFamily.InterNetwork) return hostAddress.ToString(); } return string.Empty; }