//reset invalid flags if any on Valid login public void ValidLogin(string _userId) { loginRecord jRec = new loginRecord(); jRec.AttemptCount = 0; jRec.AccountLocked = 0; setFlag(_userId, jRec); }
public bool isLocked(string _userId) { bool _islocked = false; loginRecord iRec = new loginRecord(); iRec = getFlag(_userId); int _attempCount = iRec.AttemptCount; bool _accountLocked = Convert.ToBoolean(iRec.AccountLocked); if (_accountLocked) { _islocked = true; } return(_islocked); }
//set login flags protected void setFlag(string _userID, loginRecord iRec) { if (conn == null || conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = null; string cmdStr = "UPDATE UserLogin_AD SET iAttempt = @attempt, isLocked = @locked WHERE empID = @userid"; cmd = new SqlCommand(cmdStr, conn); cmd.Parameters.Add("@userid", SqlDbType.Int); cmd.Parameters["@userid"].Value = Int32.Parse(_userID); cmd.Parameters.Add("@attempt", SqlDbType.Int); cmd.Parameters["@attempt"].Value = iRec.AttemptCount; cmd.Parameters.Add("@locked", SqlDbType.Int); cmd.Parameters["@locked"].Value = iRec.AccountLocked; cmd.ExecuteNonQuery(); conn.Close(); cmd.Dispose(); }
//get login flags from user record protected loginRecord getFlag(string _userId) { if (conn == null || conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = null; loginRecord iRec = new loginRecord(); string cmdStr = "SELECT iAttempt, isLocked FROM UserLogin_AD WHERE empID = @userid"; cmd = new SqlCommand(cmdStr, conn); cmd.Parameters.Add("@userid", SqlDbType.Int); cmd.Parameters["@userid"].Value = Int32.Parse(_userId); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { iRec.AttemptCount = dr.GetInt32(0); iRec.AccountLocked = Convert.ToInt32(dr[1]); } dr.Close(); conn.Close(); return(iRec); }
//set flags on invalid login public void InvalidLogin(string _userId) { loginRecord iRec = new loginRecord(); iRec = getFlag(_userId); int _attempCount = iRec.AttemptCount; int _accountLocked = iRec.AccountLocked; if (_attempCount < 3) { loginRecord jRec = new loginRecord(); jRec.AttemptCount = _attempCount + 1; jRec.AccountLocked = 0; setFlag(_userId, jRec); } else { loginRecord jRec = new loginRecord(); jRec.AttemptCount = _attempCount; jRec.AccountLocked = 1; setFlag(_userId, jRec); throw (new Exception("Invalid Login! You have reached maximum number of login attempts.<br/> Your account is Locked<br/> Contact your administrator to unlock your account.")); } }