Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C # how to realize the function of CheckPassword and locking the wrong password to lock the account

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to achieve CheckPassword and lock the wrong password lock account function, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

Check Password implemented by C #, and lock the account according to the number of times you enter the wrong password: if you enter the wrong password for 3 times, the login account will be locked for 5 minutes and prompted for X points and then try to log in again. If you enter it again after 5 minutes, you will enter the wrong password for a total of 5 times. The account will be permanently locked, and you need to contact the system administrator to clear the number of input errors in the database (errorcount) before you can log in. The implementation code is as follows:

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(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 consecutive password errors, please try again after" + errtime.AddMinutes (+ 5) .ToString ("yyyy-MM-dd HH:mm:ss") + ";} else {if (CheckFromLDAP (strs [1], password, strs [0])) {CPU.Models.UserInfo userInfo = CheckUser (strs [1]); if (userInfo = = null) {execRes.Status = false ExecRes.Message = "you do not have permission to operate this system!" ;} else {execRes.Status = true; execRes.Anything = userInfo; / / error count clear 0 CallEEPMethod.Execute (dbName, "sDEM2131", "UpdateUserLoginError", strs [1] .ToLower () + "," + "0" + "," + DateTime.Now.ToString ("yyyy/MM/dd HH:mm:ss"));}} else {execRes.Status = false / / times + 1 if (errcount+1 > 1) execRes.Message = "password continuous error" + (errcount+1) .ToString () + "times. The password will be locked if you enter the wrong password for five times in a row. " ; else execRes.Message = "password error!"; 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 = "continuous password error" + (errcount + 1) .ToString () + "times, please try again after" + dt0.AddMinutes (5) .ToString ("yyyy-MM-dd HH:mm:ss") + ", thanks!"; if (errcount + 1 = = 5) execRes.Message = "account password has been entered incorrectly for 5 times in a row and locked! Please contact the administrator to unlock, thanks!;} else {execRes.Status = false; execRes.Message = "account password has been entered incorrectly for 5 times in a row and has been locked! Please contact the administrator to unlock, thanks!;} else {execRes.Status = false; execRes.Message = "cannot find this account, please re-enter!";}} else {execRes.Status = false; execRes.Message = "cannot find this account, please re-enter!";}} return execRes;}

Perform Form verification according to different network domains logged in

Private bool CheckFromLDAP (string ntID, string ntPWD, string domain) / / Form authentication based on different domains logged in {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.Constructions ("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;}

Manage permissions according to different user logins

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

The above is all the contents of the article "how to achieve CheckPassword and lock the wrong password to lock your account". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report