網站首頁 編程語言 正文
C#實現的Check Password,并根據輸錯密碼的次數分情況鎖定賬戶:如果輸入錯誤3次,登錄賬戶鎖定5分鐘并提示X點X分后重試登錄。如果5分鐘后再次輸入,累計輸入錯誤密碼累計達到5次。則賬戶會被永久鎖定,需聯系系統管理員進行把數據庫中的輸入錯誤的次數(errorcount)進行清零解鎖才能登陸。實現代碼如下:
public class UserInfo1
{
public string Error_count { get; set; }
public string Error_time { get; set; }
}
public ExecutionResult CheckAccountPwd(string account, string password)
{
ExecutionResult execRes;
execRes = new ExecutionResult();
string[] strs = account.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
if (strs.Length < 2)
{
execRes.Status = false;
execRes.Message = "無效的賬號。";
}
else
{
UserInfo1 info1 = null;
execRes = CallEEPMethod.Execute(dbName, "sDEM2131", "GetUserInfo", strs[1].ToLower());
if (execRes.Status && execRes.Anything != null)
{
info1 = JsonConvert.DeserializeObject<UserInfo1>(execRes.Anything.ToString());
if (info1 != null)
{
int errcount = Convert.ToInt32(info1.Error_count);
DateTime errtime = Convert.ToDateTime(info1.Error_time);
if (errcount != 5)
{
//int errorCount
DateTime dt0 = DateTime.Now;
DateTime dt1 = errtime.AddMinutes(5);
double s = (dt1 - dt0).TotalSeconds;
if (errcount == 3 && s > 0)
{
execRes.Status = false;
execRes.Message = "密碼連續輸入錯誤3次,請于 " + errtime.AddMinutes(+5).ToString("yyyy-MM-dd HH:mm:ss") + " 之后重試,thanks!";
}
else
{
if (CheckFromLDAP(strs[1], password, strs[0]))
{
CPU.Models.UserInfo userInfo = CheckUser(strs[1]);
if (userInfo == null)
{
execRes.Status = false;
execRes.Message = "您沒有權限操作此系統!";
}
else
{
execRes.Status = true;
execRes.Anything = userInfo;
//error count 清0
CallEEPMethod.Execute(dbName, "sDEM2131", "UpdateUserLoginError", strs[1].ToLower() + ","+"0" + "," + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
}
}
else
{
execRes.Status = false;
// 次數+1
if (errcount + 1 > 1)
execRes.Message = "密碼連續輸入錯誤" + (errcount+1).ToString() + "次。密碼連續輸錯5次將鎖定!";
else
execRes.Message = "密碼輸入錯誤!";
dt0 = DateTime.Now;
CallEEPMethod.Execute(dbName, "sDEM2131", "UpdateUserLoginError", strs[1].ToLower() + "," + (errcount + 1).ToString()+"," + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
if (errcount + 1 == 3)
execRes.Message = "密碼連續輸入錯誤" + (errcount + 1).ToString() + "次,請于 " + dt0.AddMinutes(5).ToString("yyyy-MM-dd HH:mm:ss") + " 之后重試,thanks!";
if (errcount + 1 == 5)
execRes.Message = "賬號密碼連續輸入錯誤5次,已鎖定!請聯系管理員解鎖,thanks!";
}
}
}
else
{
execRes.Status = false;
execRes.Message = "賬號密碼連續輸入錯誤5次,已鎖定!請聯系管理員解鎖,thanks!";
}
}
else
{
execRes.Status = false;
execRes.Message = "找不到此賬號,請重新輸入!";
}
}
else
{
execRes.Status = false;
execRes.Message = "找不到此賬號,請重新輸入!";
}
}
return execRes;
}
根據登錄不同的網域進行Form驗證
private bool CheckFromLDAP(string ntID, string ntPWD, string domain)//根據登錄的不同網域進行Form驗證
{
bool result = false;
string strUser;
try
{
strUser = domain + "\\" + ntID;
if (domain.ToLower().Equals("gi"))
domain = "gi.compal.com";
else if (domain.ToLower().Equals("cqc_cci"))
domain = "10.140.1.1";
else if (domain.ToLower().Equals("vn"))
domain = "10.144.2.101";
else if (domain.ToLower().Equals("njp_cci"))
domain = "10.128.50.1";
else
domain = "compal.com";
DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, strUser, ntPWD);
using (DirectorySearcher searcher = new DirectorySearcher(entry))
{
searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", ntID);
SearchResult sr = searcher.FindOne();
using (SearchResultCollection results = searcher.FindAll())
{
if (results.Count > 0)
{
//if (results[0].Properties.Contains("employeeID"))
// empID = results[0].Properties["employeeID"][0].ToString();
//else
// empID = results[0].Properties["extensionattribute3"][0].ToString();
result = true;
}
}
}
}
catch (Exception ex)
{
//LogHelper.Error(ex.Message);
}
return result;
}
根據不同的用戶登錄進行權限管理
public bool CheckPermission(string controllerName, string actionName,string plant, string userID)
{
bool result = false;
//if (actionName.StartsWith("_"))
// actionName = actionName.Substring(1);
UserInfo userInfo = CheckUser(userID);
if (userInfo!=null)
{
if (controllerName == "Home")
result = true;
else if (userInfo.Permissions.Contains(controllerName))
{
if (!string.IsNullOrEmpty(plant))
{
if (userInfo.PlantCode.ToLower() == plant.ToLower() || userInfo.PlantCode == "ALL")
result = true;
}
else
result = true;
}
}
return result;
}
原文鏈接:https://www.cnblogs.com/wml-it/p/12205676.html
相關推薦
- 2022-08-24 使用chrome控制臺作為.Net的日志查看器_實用技巧
- 2022-09-04 Go語言中的函數詳解_Golang
- 2022-06-01 一篇文章徹底弄懂Python字符編碼_python
- 2022-10-16 Python3列表刪除的三種方式實現_python
- 2022-09-10 Python學習筆記嵌套循環詳解_python
- 2022-04-04 Python數據處理-導入導出excel數據_python
- 2023-04-08 React中useCallback?useMemo到底該怎么用_React
- 2023-06-21 C++析構函數內部工作機制詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支