AngularJS Login Form With ASP.NET

As per you request, I am going to make a login page in AngularJS and ASP. NET. I am trying to make this article as simple as I can. I hope you like it. Your valuable feedback is always welcome.Thanks.

Let’s start.

Please follow these steps to make this Application. Step

Create an empty MVC project

 

First, open your Visual Studio -> File -> New -> Project -> ASP.NET Web Application and click OK. Now, select MVC and OK. Now, you have created the basic project structure.

You can go though the screenshot given below for help.

You will have the directory given below.

Step 2 

Install Angular package

Install Angular package in your project, as shown in the screenshot given below.

Step 3

In Views folder loginpage, paste the code given below in login.cshtml.

  1. @ {  
  2.     ViewBag.Title = "Login Using Angular";  
  3. } < h2 > Login Using Angular < /h2> < div ng – controller = "LoginController" > < form name = "myForm"  
  4. novalidate ng – submit = "LoginForm()" > < div style = "color:green" > {  
  5.     {  
  6.         msg  
  7.     }  
  8. } < /div> < table ng – show = "!IsLoggedIn"  
  9. class = "table table-horizontal" > < tr > < td > Email / UserName: < /td> < td > < input type = "email"  
  10. ng – model = "UserModel.Email"  
  11. name = "UserEmail"  
  12. ng – class = "Submited?’ng-dirty’:”"  
  13. required autofocus class = "form-control" / > < span style = "color:red"  
  14. ng – show = "(myForm.UserEmail.$dirty || Submited ) && myForm.UserEmail.$error.required" > Please enter Email < /span> < span style = "color:red"  
  15. ng – show = "myForm.UserEmail.$error.email" > Email is not valid < /span> < /td> < /tr> < tr > < td > Password: < /td> < td > < input type = "password"  
  16. ng – model = "UserModel.Password"  
  17. name = "UserPassword"  
  18. ng – class = "Submited?’ng-dirty’:”"  
  19. required autofocus class = "form-control" / > < span style = "color:red"  
  20. ng – show = "(myForm.UserPassword.$dirty || Submited) && myForm.UserPassword.$error.required" > Password Required < /span> < /td> < /tr> < tr > < td > < /td> < td > < input type = "submit"  
  21. value = "submit"  
  22. class = "btn btn-success" / > < /td> < /tr> < /table> < /form> < /div>  
  23. @section scripts { < script src = "~/Scripts/LoginController.js" > < /script>  
  24. }  

We have to create a JavaScript login controller and added its reference, as shown below.

  1. “~/Scripts/LoginController.js”>  

Step 4 

Add Javascript Controllor

Create an Angular module in Module.js file and add the code given below.

  1. (function() {  
  2.     var myApp = angular.module("myApp", []);  
  3. })();  

Now, add another script file for Angular controller and Factory method.

Right click on Scripts folder –> add LoginController.js and write the code given below.

  1. angular.module(‘myApp’).controller(‘LoginController’function($scope, LoginService) {  
  2.     //initilize user data object  
  3.     $scope.LoginData = {  
  4.         Email: ,  
  5.         Password:   
  6.     }  
  7.     $scope.msg = "";  
  8.     $scope.Submited = false;  
  9.     $scope.IsLoggedIn = false;  
  10.     $scope.IsFormValid = false;  
  11.     //Check whether the form is valid or not using $watch  
  12.     $scope.$watch("myForm.$valid"function(TrueOrFalse) {  
  13.         $scope.IsFormValid = TrueOrFalse; //returns true if form valid  
  14.     });  
  15.     $scope.LoginForm = function() {  
  16.         $scope.Submited = true;  
  17.         if ($scope.IsFormValid) {  
  18.             LoginService.getUserDetails($scope.UserModel).then(function(d) {  
  19.                 debugger;  
  20.                 if (d.data.Email != null) {  
  21.                     debugger;  
  22.                     $scope.IsLoggedIn = true;  
  23.                     $scope.msg = "You successfully Loggedin Mr/Ms " + d.data.FullName;  
  24.                 } else {  
  25.                     alert("Invalid credentials buddy! try again");  
  26.                 }  
  27.             });  
  28.         }  
  29.     }  
  30. }).factory("LoginService"function($http) {  
  31.     //initilize factory object.  
  32.     var fact = {};  
  33.     fact.getUserDetails = function(d) {  
  34.         debugger;  
  35.         return $http({  
  36.             url: ‘/Home/VerifyUser,  
  37.             method: ‘POST’,  
  38.             data: JSON.stringify(d),  
  39.             headers: {  
  40.                 ‘content-type’‘application/json’  
  41.             }  
  42.         });  
  43.     };  
  44.     return fact;  
  45. });  

Step 5

Add New Action Method in HomeController to verify the user.

  1. public ActionResult VerifyUser(UserModel obj)  
  2. {  
  3.     DatabaseEntities db = new DatabaseEntities();  
  4.     var user = db.Users.Where(x => x.Email.Equals(obj.Email) && x.Password.Equals(obj.Password)).FirstOrDefault();  
  5.     return new JsonResult {  
  6.         Data = user, JsonRequestBehavior = JsonRequestBehavior.AllowGet  
  7.     };  
  8. }  

Step 6 

Add a Model class to the solution

Right click Models –> Add –> Class and name it UserModel.cs.

You can add usermodel, as shown below.

  1. namespace LoginUsingAngular.Models  
  2. {  
  3.     public class UserModel {  
  4.         public string Email {  
  5.             get;  
  6.             set;  
  7.         }  
  8.         public string Password {  
  9.             get;  
  10.             set;  
  11.         }  
  12.     }  
  13. }  

Take a look at what the ng-Model, ng-show and ng-submit means, which is used in login.cshtml view given above.

  • ng-Model ngModel is an Angular Directive. It is used for two way binding data from View to Controller and Controller to View.
  • ng-show ngShow allows to display or hide the elements, which are based on the expression provided to ngShow attribute.
  • ng-submit ng-submit prevents the default action of the form and binds Angular function to onsubmit events. This is invoked when the form is submitted.
  • $dirty It is an Angular built in property. It will be true, if the user interacts with the form, else false.

There are many validation properties in Angular like $invalid, $submitted, $pristine, $valid and $error.

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

TOP