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

How to realize password Verification and wrong password account locking in C #

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

Share

Shulou(Shulou.com)05/31 Report--

This article Xiaobian for you to introduce in detail "C# how to achieve password authentication and wrong password account locking", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "C# how to achieve password authentication and input wrong password account locking" can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

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 = "password has been entered incorrectly for 3 times in a row. Please try again after" + 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 = "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 = "incorrect password!"; 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 = "password continuous 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 entered incorrectly for 5 times in a row, locked! Please contact the administrator to unlock, thanks! ";}} else {execRes.Status = false ExecRes.Message = "account password entered incorrectly for 5 times in a row, locked!" Please contact the administrator to unlock, thanks!;}} else {execRes.Status = false; execRes.Message = "this account cannot be found, please re-enter it!" }} else {execRes.Status = false; execRes.Message = "this account cannot be found, please re-enter it!";}} 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. 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;}

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 (userInfotainment 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 } after reading this, the article "how to implement password Verification and wrong password account locking in C#" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, 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