Limit user Login attempt using AngularJS in ASP.Net MVC

Last Reply one year ago By dharmendr

Posted one year ago

Hi,

I have one login page

If particular User enters password 3 times wrong then block that user and save that user name details to another table

Login Page with Remember Me using AngularJS in ASP.Net MVC

Could you please help me

Posted one year ago

Hi mahesh213,

Using the below link i have modified the code.

Login Page with Remember Me using AngularJS in ASP.Net MVC

I have used below Table to save the user attemp for blocking.

CREATE TABLE UserLoginDetails
(
	ID INT IDENTITY PRIMARY KEY,
	UserName VARCHAR(50),
	Attempt INT
)

View

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Index</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
    <script type="text/javascript">
        var app = angular.module("myApp", []);
        app.controller("myCntrl", ['$scope', '$http', 'myService', function ($scope, $http, myService) {
            $scope.UserName = "Admin";
            $scope.Password = "12345";
            $scope.LoginCheck = function () {
                var User = {
                    UserName: $scope.UserName,
                    Password: $scope.Password,
                    RememberMe: $scope.Checked
                };
                var getData = myService.UserLogin(User);
                getData.then(function (msg) {
                    if (msg.data.Status == "-1") {
                        $scope.msg = "Username or Password is Incorrect !";
                    } else if (msg.data.Status == "-2") {
                        $scope.msg = "You have been blocked due to 3 wrong attemp !";
                    } else {
                        window.location.href = "/Home/Success";
                    }
                });
            }
        } ]);
        app.service("myService", function ($http) {
            this.UserLogin = function (User) {
                var response = $http({
                    method: "post",
                    url: "/Home/Login",
                    params: User,
                    dataType: "json"
                });
                return response;
            }
        });
    </script>
</head>
<body ng-app="myApp" ng-controller="myCntrl">
    <div class="container" align="center">
        <div class="form-horizontal">
            <div class="row">
                UserName:<input type="text" ng-model="UserName" name="username" class="form-control" /><br />
                Password:
                <input type="password" ng-model="Password" name="password" class="form-control" /><br />
                <input type="checkbox" ng-model="Checked" />&nbsp;Remember Me.
                <br />
                <br />
                <button type="submit" id="signin" name="name" ng-click="LoginCheck()" class="btn btn-primary">
                    Proceed</button>
                <br />
                <span style="color: Red">{{msg}}</span>
            </div>
        </div>
    </div>
</body>
</html>

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Default()
    {
        if (Request.Cookies["LoginDetail"] != null)
        {
            return View();
        }
        else
        {
            return View("Index");
        }
    }

    [HttpPost]
    public string Login(Encrypt data)
    {
        LoginEntities db = new LoginEntities();
        string username = data.UserName;
        string password = data.Password;
        bool remberMe = data.RememberMe;
        var userAttempt = db.UserLoginDetails.Where(x => x.UserName == username).FirstOrDefault();
        if (userAttempt == null || userAttempt.Attempt < 3)
        {
            var user = db.Users.Where(u => u.Username == username).FirstOrDefault();
            if (user != null)
            {
                if (user.Password == password)
                {
                    Session["Id"] = user.UserId;
                    Session["UserName"] = user.Username;
                    Session["Password"] = user.Password;
                    data.Status = 1;
                    data.UserId = user.UserId;
                    if (userAttempt != null)
                    {
                        db.UserLoginDetails.DeleteObject(db.UserLoginDetails.Where(x => x.UserName == username).FirstOrDefault());
                        db.SaveChanges();
                    }

                    if (remberMe)
                    {
                        // Add Cookies.
                        HttpCookie mycookie = new HttpCookie("LoginDetail");
                        mycookie.Values["Username"] = user.Username;
                        mycookie.Values["Password"] = user.Password;
                        mycookie.Expires = System.DateTime.Now.AddDays(365);
                        Response.Cookies.Add(mycookie);
                    }
                }
                else
                {
                    if (userAttempt == null)
                    {
                        UserLoginDetail details = new UserLoginDetail();
                        details.UserName = username;
                        details.Attempt = 1;
                        db.UserLoginDetails.AddObject(details);
                        db.SaveChanges();
                    }
                    else
                    {
                        userAttempt.Attempt = userAttempt.Attempt + 1;
                        db.SaveChanges();
                    }
                    data.Status = -1;
                }
            }
        }
        else
        {
            data.Status = -2;
        }

        System.Web.Script.Serialization.JavaScriptSerializer se = new System.Web.Script.Serialization.JavaScriptSerializer();
        return se.Serialize(data);
    }

    public ActionResult Success()
    {
        if (Request.Cookies["LoginDetail"] != null)
        {
            return View();
        }
        else
        {
            return View("Index");
        }
    }

    [HttpPost]
    public ActionResult Logout()
    {
        // Remove Cookies.
        HttpCookie mycookie = Request.Cookies["LoginDetail"];
        mycookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(mycookie);
        return View("Index");
    }
}

Screenshot