Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 what

What I have tried:

C#
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;
        }
Posted
Updated 1-Sep-19 19:51pm

1 solution

The best way is to keep a DB table which records all login attempts, and the successfulness of that attempt. It's pretty simple: all it needs is four columns (ID, UserID, Timestamp, Success). Each time an attempt is made, add a new row. You can then monitor failures with a simple SELECT from your DB on a user-by-user basis.
 
Share this answer
 
Comments
ahmed_sa 2-Sep-19 3:59am    
can you give me more details if possible please ?
OriginalGriff 2-Sep-19 4:02am    
About what?
Which part of it is difficult to understand?
ahmed_sa 2-Sep-19 4:15am    
You can then monitor failures with a simple SELECT from your DB on a user-by-user basis.
OriginalGriff 2-Sep-19 4:42am    
You know how to use SELECT from a database, yes?
So SELECT all rows for the specific user ("... WHERE UserID = ..." or similar) ORDER BY the timestamp DESC and it will return all rows with the times and success / fail code. From that you can count 'em, do anything you like with them.
Or you can return only failed attempts since the last success with a subquery.
Pretty trivial SQL, so what part is difficult for you?
ahmed_sa 6-Sep-19 22:54pm    
i will ask you again can you show me psudo code please i don following steps
1- create table loginattempt UserId,TimeStamp,success
2- select * from loginattempt where userid=@userid order by timestamp
what i do after that please ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900