Background

In our simple life we have many accounts, like bank account, insurance account, Facebook account, Gmail account and so on. I forget my password regularly but thank you developers for providiing me a password recovery capability. Without a password recovery capability I would have no way to remember my password. In this article I have a single table tbl_ALogin.
  1. select * from tbl_ALogin
  1. LoginID UserName Password Salt LoginDateTime LastLoginTime IsBlocked AccessCounter
  2. 1 abc 74e9f6722b6df40630d8d7249078453ad1af05c8 a8947f78d0ca294c 2012-11-14 10:03:53.070 2012-11-14 10:03:53.070 0 0
  3. 5 cde 62ee78aa3cb90bde955166e6478d4f807f6dea0c 90f62957f3e9bab 2014-12-20 12:46:46.610 2012-11-14 13:34:18.623 0 0
We can see the preceding table has two columns, one is "IsBlocked" and the other is "AccessCounter". These columns provide access and not access permission.
I assume cde user name has the password admin123.
For example, now enter the wrong password.
The following shows what has changed in the table after the wrong password.
  1. LoginID UserName Password Salt LoginDateTime LastLoginTime IsBlocked AccessCounter
  2. 1 abc 74e9f6722b6df40630d8d7249078453ad1af05c8 a8947f78d0ca294c 2012-11-14 10:03:53.070 2012-11-14 10:03:53.070 0 0
  3. 5 cde 62ee78aa3cb90bde955166e6478d4f807f6dea0c 90f62957f3e9bab 2014-12-20 12:46:46.610 2012-11-14 13:34:18.623 0 1
Where is the check in Stored Procedure access counter:
  1. if (DATEDIFF(n,@logintime,getdate())<30)
  2. set @counter=@counter+1
If we enter the wrong password again then the access counter is incremented.
  1. LoginID UserName Password Salt LoginDateTime LastLoginTime IsBlocked AccessCounter
  2. 1 abc 74e9f6722b6df40630d8d7249078453ad1af05c8 a8947f78d0ca294c 2012-11-14 10:03:53.070 2012-11-14 10:03:53.070 0 0
  3. 5 cde 62ee78aa3cb90bde955166e6478d4f807f6dea0c 90f62957f3e9bab 2014-12-20 12:46:46.610 2012-11-14 13:34:18.623 0 2
If we enter the wrong password then again our account is blocked as in the following:
  1. if(@counter>2)
  2. begin
  3. update tbl_ALogin set isblocked=1,AccessCounter=@counter,Logindatetime=getdate() where LoginID=@tmpuserid
  4. set @Succ=-1
  5. end
After the blocked account the following shows how it is displayed:
  1. LoginID UserName Password Salt LoginDateTime LastLoginTime IsBlocked AccessCounter
  2. 1 abc 74e9f6722b6df40630d8d7249078453ad1af05c8 a8947f78d0ca294c 2012-11-14 10:03:53.070 2012-11-14 10:03:53.070 0 0
  3. 5 cde 62ee78aa3cb90bde955166e6478d4f807f6dea0c 90f62957f3e9bab 2014-12-20 12:46:46.610 2012-11-14 13:34:18.623 1 3
The following is the complete procedure of the user login.
  1. ALTER PROCEDURE [dbo].[proc_UserLogin]
  2. -- Add the parameters for the stored procedure here
  3. @username varchar(100),
  4. @password varchar(50),
  5. @succ int out,
  6. @LoginID int out
  7. AS
  8. BEGIN
  9. -- SET NOCOUNT ON added to prevent extra result sets from
  10. -- interfering with SELECT statements.
  11. SET NOCOUNT ON;
  12. declare @tmpuserid int
  13. declare @isblocked bit
  14. declare @logintime datetime
  15. declare @validpassword varchar(50)
  16. declare @counter int
  17. -- Insert statements for procedure here
  18. select @tmpuserid=LoginID,@validpassword=Password,@isblocked=isblocked,@logintime=Logindatetime,@counter=isnull(AccessCounter,0) from tbl_AdminLogin where username=@Username
  19. --select @tmpuserid=LoginID,@validpassword=Password,@isblocked=isblocked,@logintime=Logindatetime,@counter=isnull(AccessCounter,0) from tbl_AdminLogin where username='admin'
  20. -- print DATEDIFF(n,@logintime,getdate())
  21. if (@isblocked=1 and (DATEDIFF(n,@logintime,getdate())>30) and @validpassword=@Password)
  22. begin
  23. update tbl_AdminLogin set isblocked=0,Logindatetime=getdate(),AccessCounter=0 where LoginID=@tmpuserid
  24. set @succ=1
  25. set @LoginID=@tmpuserid
  26. end
  27. else
  28. begin
  29. if (@isblocked=1 and @validpassword<>@Password)
  30. set @Succ=-1
  31. else
  32. if(@isblocked=1)
  33. set @Succ=-1
  34. else
  35. begin
  36. if (@isblocked=0 and @validpassword=@Password)
  37. begin
  38. update tbl_AdminLogin set isblocked=0,Logindatetime=getdate(),AccessCounter=0 where LoginID=@tmpuserid
  39. set @succ=1
  40. set @LoginID=@tmpuserid
  41. end
  42. else
  43. begin
  44. if (DATEDIFF(n,@logintime,getdate())<30)
  45. set @counter=@counter+1
  46. if(@counter>2)
  47. begin
  48. update tbl_AdminLogin set isblocked=1,AccessCounter=@counter,Logindatetime=getdate() where LoginID=@tmpuserid
  49. set @Succ=-1
  50. end
  51. else
  52. begin
  53. update tbl_AdminLogin set AccessCounter=@counter where LoginID=@tmpuserid
  54. set @succ=-2
  55. end
  56. end
  57. end
  58. end
  59. -- print @succ
  60. END