欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯(cuò)密碼賬戶(hù)鎖定

 更新時(shí)間:2022年04月14日 09:30:58   作者:農(nóng)碼一生  
這篇文章介紹了C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯(cuò)密碼賬戶(hù)鎖定的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

C#實(shí)現(xiàn)的Check Password,并根據(jù)輸錯(cuò)密碼的次數(shù)分情況鎖定賬戶(hù):如果輸入錯(cuò)誤3次,登錄賬戶(hù)鎖定5分鐘并提示X點(diǎn)X分后重試登錄。如果5分鐘后再次輸入,累計(jì)輸入錯(cuò)誤密碼累計(jì)達(dá)到5次。則賬戶(hù)會(huì)被永久鎖定,需聯(lián)系系統(tǒng)管理員進(jìn)行把數(shù)據(jù)庫(kù)中的輸入錯(cuò)誤的次數(shù)(errorcount)進(jìn)行清零解鎖才能登陸。實(shí)現(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 = "無(wú)效的賬號(hào)。";
            }
            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ù)輸入錯(cuò)誤3次,請(qǐng)于 " + 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 = "您沒(méi)有權(quán)限操作此系統(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ù)輸入錯(cuò)誤" + (errcount+1).ToString() + "次。密碼連續(xù)輸錯(cuò)5次將鎖定!";
                                    else
                                        execRes.Message = "密碼輸入錯(cuò)誤!";
                                    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ù)輸入錯(cuò)誤" + (errcount + 1).ToString() + "次,請(qǐng)于 " + dt0.AddMinutes(5).ToString("yyyy-MM-dd HH:mm:ss") + " 之后重試,thanks!";
                                    if (errcount + 1 == 5)
                                        execRes.Message = "賬號(hào)密碼連續(xù)輸入錯(cuò)誤5次,已鎖定!請(qǐng)聯(lián)系管理員解鎖,thanks!";
                                }
                            }
                        }
                        else
                        {
                            execRes.Status = false;
                            execRes.Message = "賬號(hào)密碼連續(xù)輸入錯(cuò)誤5次,已鎖定!請(qǐng)聯(lián)系管理員解鎖,thanks!";
                        }
                    }
                    else
                    {
                        execRes.Status = false;
                        execRes.Message = "找不到此賬號(hào),請(qǐng)重新輸入!";
                    }
                }
                else
                {
                    execRes.Status = false;
                    execRes.Message = "找不到此賬號(hào),請(qǐng)重新輸入!";
                }
            }
            return execRes;
        }

根據(jù)登錄不同的網(wǎng)域進(jìn)行Form驗(yàn)證

private bool CheckFromLDAP(string ntID, string ntPWD, string domain)//根據(jù)登錄的不同網(wǎng)域進(jìn)行Form驗(yàn)證
        {
            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ù)不同的用戶(hù)登錄進(jìn)行權(quán)限管理

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;
        }

到此這篇關(guān)于C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯(cuò)密碼賬戶(hù)鎖定的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論