6 Replies - 547 Views - Last Post: 07 September 2019 - 10:12 AM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

How to do count login attempt within period ?

Posted 01 September 2019 - 07:59 PM

I work on web api login functionality and i need to make count login attempt within period
How to do it .
are that will made on separate action or inside postuserlogin action
public class Users
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string MessageStatus { get; set; }
        public int StatusCode { get; set; }
        public string StatusText { get; set; }
        public string IpAddress { get; set; }
        public List<Branches> Branches { get; set; }
        public string LoginTime { get; set; }
        public string AccessToken { get; set; }
    }
[HttpPost(Contracts.ApiRoutes.Login.PostUserLogin)]
        public  IActionResult PostUserLogins([FromBody]Users user)
        {
            string JsonResults = "";
            int LoginStatus = _AuthunticateService.PostUserLogin(user.UserName,user.Password ,out DataTable dtBranches,out string errorMessage, out int statusCode);
            
             if(LoginStatus == 1)
            {
                user.StatusCode = statusCode;
                user.StatusText = "failed";
                user.MessageStatus = "login failed, incorrect username or password";
               

            }
            else if(LoginStatus == 2)
            {
                user.StatusCode = statusCode;
                user.StatusText = "failed";
                user.MessageStatus = "password has expired!";
                
            }
            return Ok(JsonResults);
        }

public int PostUserLogin(string UserId, string Password, out DataTable dtGetBranches, out string errorMessage, out int statusCode)
        {
            statusCode = 0;
            bool IsUserValid = false;
            dtGetBranches = null;
            errorMessage = null;

            if (!string.IsNullOrEmpty(UserId))
            {

          //how to check login prompt count 
               // check user validation
                if (IsUserValid)
                {
                    statusCode = 0;
                    IsUserValid = true;
                  //get user branches 
//get user id 
                }
                else
                {
                    statusCode = 1;
                    IsUserValid = false;
                    errorMessage = string.Format("Invalid User Name {0} Or Password {1}", UserId, Password);
                }

            }
            return statusCode;
        }



Is This A Good Question/Topic? 0
  • +

Replies To: How to do count login attempt within period ?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15361
  • View blog
  • Posts: 61,607
  • Joined: 12-June 08

Re: How to do count login attempt within period ?

Posted 01 September 2019 - 08:48 PM

I typically store that in the database. Count and last attempt datetime. Successful login clears both.
Was This Post Helpful? 0
  • +
  • -

#3 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: How to do count login attempt within period ?

Posted 02 September 2019 - 12:55 AM

if possible can you clear what you write or mention some details
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15361
  • View blog
  • Posts: 61,607
  • Joined: 12-June 08

Re: How to do count login attempt within period ?

Posted 02 September 2019 - 06:12 AM

Some where in your code a call to a database happens, right? Where you either compare the credentials given to what is there or pull some back before returning data to the caller. It's about there I have a call to either increment a 'last log in attempt count' and add a 'last log in attempt' date time, or clear it based on a successful log in.
Was This Post Helpful? 0
  • +
  • -

#5 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: How to do count login attempt within period ?

Posted 06 September 2019 - 08:44 PM

I need to make count login attempt within period but i dont know are this logic is correct or wrong or something missed

if any thing wrong please help me or tell me what is remaining ?

i need to block user when logincount > maxnumber with failed login

const int MaxNumberOfFailedAttemptsToLogin = 3;
const int BlockMinutesAfterLimitFailedAttemptsToLogin = 15;

public class Users
    {
       
        public DateTime? LastLoginAttemptAt { get; set; }
        public int LoginFailedAttemptsCount { get; set; }
    }

public void CountLoginAttempt(string UserId, string Password,out bool Status)
        {
           
            usr.LoginFailedAttemptsCount = 0;
            usr.LastLoginAttemptAt = DateTime.Now;
            Status = true;
           

            string getCountLogin = @"select  LastLoginAttemptAt , LoginFailedAttemptsCount from Users where Active = 1 AND UserId = @UserID";
            DataTable dtgetloginattempt = get result of query getCountLogin
            if (dtgetloginattempt.Rows.Count > 0)
            {
                usr.LoginFailedAttemptsCount = Utilities.ObjectConverter.ConvertToInteger(dtgetloginattempt.Rows[0]["LoginFailedAttemptsCount"]);
                usr.LastLoginAttemptAt = Utilities.ObjectConverter.ConvertToDateTime(dtgetloginattempt.Rows[0]["LastLoginAttemptAt"]);
            }
            if (usr.LoginFailedAttemptsCount > MaxNumberOfFailedAttemptsToLogin
            && usr.LastLoginAttemptAt.HasValue
            && DateTime.Now < usr.LastLoginAttemptAt.Value.AddMinutes(BlockMinutesAfterLimitFailedAttemptsToLogin))
            {
                // Login is blocked, need to break the process.
                // Return error message "Your account was blocked 
                // for a 15 minutes, please try again later."
                Status = false;
                return;
            }
           
            
           
            var validUserNameAndPassword = UserManager.IsValidUser(UserId, EncryptedPassword);
            if (!validUserNameAndPassword)
            {
                // Invalid password, need to update the number of attempts.
     
                usr.LoginFailedAttemptsCount++;
                

                
                if(usr.LoginFailedAttemptsCount==1)
                {
                    string Sql = @"update Users set LastLoginAttemptAt='" + DateTime.Now.ToString("yyyy/MM/dd HH:mm") + "' , LoginFailedAttemptsCount=" + usr.LoginFailedAttemptsCount + " where Active = 1 AND UserId = @UserID";
                    
                }
                else
                {
                    string Sql = @"update Users set  LoginFailedAttemptsCount=" + usr.LoginFailedAttemptsCount + " where Active = 1 AND UserId = @UserID";
                    
                }
                
                // Update(login);
                // Return error message "Invalid username or password"
                return;
            }
            else
            {
                usr.LoginFailedAttemptsCount = 0;
            
                string Sql = @"update Users set LastLoginAttemptAt=null , LoginFailedAttemptsCount=0 where Active = 1 AND UserId = @UserID ";
                
                Status = true;
                // Update(login);
                // Success!
            }
        }

Are this logic above is correct or have some thing wrong ?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15361
  • View blog
  • Posts: 61,607
  • Joined: 12-June 08

Re: How to do count login attempt within period ?

Posted 07 September 2019 - 08:37 AM

Do not create duplicate topics.


Also copy/pasted spammed here, here, and here..
https://forums.asp.n...way+is+correct+
https://social.msdn....m=csharpgeneral
https://www.c-sharpc...y-this-way-is-c
Was This Post Helpful? 0
  • +
  • -

#7 ahmedba   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 220
  • Joined: 24-January 14

Re: How to do count login attempt within period ?

Posted 07 September 2019 - 10:12 AM

thank you for reply
can any one help me are code above have wrong on logic when count failed login attempt within period
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1