Learn MVC Using Angular Role Based Login

ASP.NET

Introduction

This article demonstrates how to create Angular login using UI-Route in MVC. Learn how to set up role based login in Angular JS using VS 2017.

Angular JS is very powerful framework for single page applications and when we are creating SPA route it will be very important. In this section I am going to use UI-Route and roles are Manager & Employee.

My Articles

Step 1

Open Visual Studio 2017 and go to File>New>Project. The New project window will be open.

ASP.NET

Select web template under the ASP.NET Web Application Framework. Now,  a popup opens; choose MVC Templates for our application.

ASP.NET

Once the project is created in solution explorer, it will open like the picture below.

ASP.NET

Step 2

ASP.NET

Create a new folder in Solution Explorer, name it “App”, and create user role based folders (Manager & Employee). Create the required HTML and JavaScript files named as shown in the picture. Here, I am using UI-Route for Angular login, because UI-Route can maintain the “State” of application. If you want to learn about Angular UI-Route, click here.

Step 3

Create HTML page login.html and paste the following code.

  1. <div class="container">  
  2.     <div class="row" style="margin-top:5%">  
  3.         <div class="col-sm-6 col-md-4 col-md-offset-4">  
  4.             <div class="panel panel-default boxshadow">  
  5.                 <div class="panel-heading" style="color:#ffffff; background-color: #3276b1;">  
  6.                     <strong> Sign in to continue</strong>  
  7.                 </div>  
  8.                 <div class="panel-body">  
  9.                     <form role="form" >  
  10.                         <fieldset>  
  11.                             <div class="row">  
  12.                                 <div class="col-md-4"></div>  
  13.                                 <div class="col-md-4">  
  14.                                     <div class="center">  
  15.   
  16.                                         <img class="img-circle" style="height:100px;width:100px" src="../../Content/user/02.jpg" />  
  17.                                           
  18.                                     </div>  
  19.                                 </div>  
  20.                                 <div class="col-md-4"></div>  
  21.   
  22.   
  23.                             </div>  
  24.                             <br />  
  25.                             <div class="row">  
  26.                                 <div class="col-sm-12 col-md-10  col-md-offset-1 ">  
  27.                                     <span class="alert-danger">{{Message}}</span>  
  28.                                     <div class="form-group">  
  29.                                         <div class="input-group">  
  30.                                             <span class="input-group-addon">  
  31.                                                 <i class="fa fa-user">User Name</i>  
  32.                                             </span>  
  33.                                             <input class="form-control txtheight" placeholder="Username" id="UserName" ng-model="UserName" name="UserName" type="text" autofocus>  
  34.                                         </div>  
  35.                                     </div>  
  36.                                     <br />  
  37.   
  38.                                     <div class="form-group">  
  39.                                         <div class="input-group">  
  40.                                             <span class="input-group-addon">  
  41.                                                 <i class="fa fa-lock">Password</i>  
  42.                                             </span>  
  43.                                             <input class="form-control txtheight" placeholder="Password" id="Password" ng-model="Password" name="Password" type="password" value="">  
  44.                                         </div>  
  45.                                     </div>  
  46.                                     <br />  
  47.                                     <div class="form-group">  
  48.                                         <input type="button" ng-click="login()" id="loginclick" class="btn btn-lg btn-primary btn-block" value="Sign in">  
  49.                                     </div>  
  50.                                 </div>  
  51.                             </div>  
  52.                         </fieldset>  
  53.                     </form>  
  54.                 </div>  
  55.             </div>  
  56.         </div>  
  57.     </div>  
  58. </div>  
  59. <style>  
  60.     #dis {  
  61.         color: orangered;  
  62.     }  
  63.   
  64.     .txtheight {  
  65.         height: 31px;  
  66.     }  
  67. </style>   

Now, create loginController.js Controller for login screen.

  1. uiroute.controller('LoginController'function ($scope, $state) {  
  2.   
  3.     $scope.login=function()  
  4.     {  
  5.         if ($scope.UserName === '' || $scope.UserName == undefined)  
  6.         {  
  7.             $scope.Message = "Username is empty"  
  8.             return;  
  9.         }  
  10.   
  11.         if ($scope.Password === '' || $scope.Password == undefined) {  
  12.             $scope.Message = "Password is empty"  
  13.             return;  
  14.         }  
  15.         if ($scope.UserName.toUpperCase() == 'MANAGER')  
  16.         {  
  17.             if($scope.Password=='1')  
  18.             {  
  19.                 $state.go('manager')  
  20.             }  
  21.         }  
  22.         else if ($scope.UserName.toUpperCase() == 'EMPLOYEE') {  
  23.             if ($scope.Password == '1') {  
  24.                 $state.go('employee')  
  25.             }  
  26.         }  
  27.         else  
  28.         {  
  29.             $scope.Message = "Username or Password is empty"  
  30.         }  
  31.     }  
  32.   
  33. });  

Here, I am done with validation and hard code for login users. I have defined the two different roles.

  1. Manager

    1. Username is “manager” and password is “1”.

  2. Employee

    1. Username is “employee” and password is “1”.

Step 4

Create the "Manager Home" screen in “Manager” folder with the file name home.html

  1. <div class="jumbotron text-center">  
  2.     <h1>Manager Home</h1>  
  3.     <a ui-sref=".list" class="btn btn-primary"> View </a>  
  4.     <a class="btn btn-danger" ui-sref="login"> Logout</a>  
  5. </div>  
  6. <div ui-view></div>  

Then, create Employee Home Screen in “Employee” folder's home.html

  1. <div class="jumbotron text-center">  
  2.     <h1>Employee Home</h1>  
  3.     <a ui-sref=".list" class="btn btn-primary"> View</a>  
  4.     <a class="btn btn-danger" ui-sref="login"> Logout</a>  
  5. </div>  
  6. <div ui-view></div>  

Step 5

When setting up states with UI-Route, we inject $stateProvider from the UI-Route module and set up the “/login” and set as default. Don’t forget to link in _layout.cshtml.

  1. uiroute.config(function ($stateProvider, $urlRouterProvider){  
  2.     $urlRouterProvider.otherwise('/login');  
  3.     $stateProvider  
  4.         // State managing    
  5.          .state('login', {  
  6.              url: '/login',  
  7.              templateUrl: '/App/Test/login.html',  
  8.              controller: 'LoginController'  
  9.          })  
  10.         //Manager Role  
  11.         .state('manager', {  
  12.             url: '/manager',  
  13.             templateUrl: '/App/Manager/home.html'  
  14.         })  
  15.          .state('manager.list', {  
  16.              url: '/list',  
  17.              templateUrl: '/App/Test/dataList.html',  
  18.              controller: 'CarController'  
  19.          })  
  20.          //Manager Role  
  21.         .state('employee', {  
  22.             url: '/employee',  
  23.             templateUrl: '/App/Employee/home.html'  
  24.         })  
  25.         .state('employee.list', {  
  26.             url: '/list',  
  27.             templateUrl: '/App/Test/homelist.html',  
  28.             controller: function ($scope) {  
  29.                 $scope.Language = ['C#''VB''JavaScript''PHP'];  
  30.             }  
  31.         });  
  32.           
  33.          
  34. });  

Step 6

Run the project or click (F5). Application will open in your default browser.

ASP.NET

First, enter the manager role’s username & password.

ASP.NET

Home screen opens based on manager’s details. If we click the view button, it opens one more View in the manage state.

ASP.NET

Click Logout button. It automatically goes to default login View. Now, let's enter the employee role’s Username and password.

ASP.NET

Now, employee screen will appear. Click the View button and it will show the list of languages.

ASP.NET

This sample article will be helpful for your role based login in Angular JS application.

Conclusion

In this article, we have seen MVC using Angular UI-Route role based login. If you have any queries, please ask me in C# Corner comments section. Happy coding.

Up Next
    Ebook Download
    View all
    Learn
    View all