這篇文章給大家分享的是有關C#怎么實現(xiàn)Check Password和鎖定輸錯密碼鎖定賬戶功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

C#實現(xiàn)的Check Password,并根據(jù)輸錯密碼的次數(shù)分情況鎖定賬戶:如果輸入錯誤3次,登錄賬戶鎖定5分鐘并提示X點X分后重試登錄。如果5分鐘后再次輸入,累計輸入錯誤密碼累計達到5次。則賬戶會被永久鎖定,需聯(lián)系系統(tǒng)管理員進行把數(shù)據(jù)庫中的輸入錯誤的次數(shù)(errorcount)進行清零解鎖才能登陸。實現(xiàn)代碼如下:
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 = "密碼連續(xù)輸入錯誤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 = "您沒有權限操作此系統(tǒng)!";
}
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;
// 次數(shù)+1
if (errcount + 1 > 1)
execRes.Message = "密碼連續(xù)輸入錯誤" + (errcount+1).ToString() + "次。密碼連續(xù)輸錯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 = "密碼連續(xù)輸入錯誤" + (errcount + 1).ToString() + "次,請于 " + dt0.AddMinutes(5).ToString("yyyy-MM-dd HH:mm:ss") + " 之后重試,thanks!";
if (errcount + 1 == 5)
execRes.Message = "賬號密碼連續(xù)輸入錯誤5次,已鎖定!請聯(lián)系管理員解鎖,thanks!";
}
}
}
else
{
execRes.Status = false;
execRes.Message = "賬號密碼連續(xù)輸入錯誤5次,已鎖定!請聯(lián)系管理員解鎖,thanks!";
}
}
else
{
execRes.Status = false;
execRes.Message = "找不到此賬號,請重新輸入!";
}
}
else
{
execRes.Status = false;
execRes.Message = "找不到此賬號,請重新輸入!";
}
}
return execRes;
}根據(jù)登錄不同的網(wǎng)域進行Form驗證
private bool CheckFromLDAP(string ntID, string ntPWD, string domain)//根據(jù)登錄的不同網(wǎng)域進行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;
}根據(jù)不同的用戶登錄進行權限管理
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;
}C#是一個簡單、通用、面向對象的編程語言,它由微軟Microsoft開發(fā),繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優(yōu)雅的語法風格、創(chuàng)新的語言特性和便捷的面向組件編程從而成為.NET開發(fā)的選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關鍵功能。
感謝各位的閱讀!關于“C#怎么實現(xiàn)Check Password和鎖定輸錯密碼鎖定賬戶功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
標題名稱:C#怎么實現(xiàn)CheckPassword和鎖定輸錯密碼鎖定賬戶功能-創(chuàng)新互聯(lián)
文章轉載:http://chinadenli.net/article24/hgpje.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、電子商務、微信小程序、手機網(wǎng)站建設、域名注冊、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)