Introduction
  • When we starts with registering users on any application or website.One thing is to care about that is security of data.
  • Passwords is the most sensitive information to travel throughout the internet ,so that should be securely transfered to the internet.
  • This article tells how to encrypt the password and registers the user with other information.
  • ADO.NET model is used as data access technique here,MVC is used with angularjs.
Create database and tables
  • Firstly Create database named TestData.
  • After creating the database create a table with the name Logins.


  • Set the Id column as primary key and auto increment id in every table.
  • That's it with the database.
Download JS and CSS files from given links

Are you new with AngularJS ?

  • Visit this link for more information on AngularJS.
Start writing code
  • Open the VS2012 ,Create New ASP.Net MVC4 Web Application and give the Name AuthenticationAngularMvcApp.
  • Go to Models Folder in the solution explorer of visual studio.
  • Right click the Models folder and find ADD >>> ADO.NET Entity Data Model. Select ADO.NET Entity Data Model.
  • Provide the name DbModel. After that pop up will appear .


  • Select generate from database, click Next.


  • In the given box type entity name as SahilEntities and After that click New connection.


  • Select Sql server authentication and fill the credentials like server name ,user name ,password,and then select your database from the database list.

  • Check the checkboxes of tables and click on finish.


Register CSS and JS in Bundle.Config
  • Go to the App_Start folder find BundleConfig.cs write the below given code.


  1. using System.Web;
  2. using System.Web.Optimization;
  3. namespace AuthenticationAngularMvc
  4. {
  5. public class BundleConfig
  6. {
  7. public static void RegisterBundles(BundleCollection bundles)
  8. {
  9. RegisterScript(bundles);
  10. RegisterStyle(bundles);
  11. }
  12. public static void RegisterScript(BundleCollection bundles)
  13. {
  14. bundles.Add(new ScriptBundle("~/js")
  15. .Include("~/Scripts/jquery-{version}.js")
  16. .Include("~/Scripts/jquery-ui-{version}.js")
  17. .Include("~/Scripts/bootstrap.js"));
  18. }
  19. public static void RegisterStyle(BundleCollection bundles)
  20. {
  21. bundles.Add(new StyleBundle("~/css")
  22. .Include("~/Content/bootstrap.css")
  23. .Include("~/Content/site.css"));
  24. }
  25. }
  26. }
Utility class to encrypt password
  • Right click on the Refrence folder ,select Manage NuGet Packages.
  • Search for Bcrypt,install by click on install button.
  • Bcrypt is a cross platform file encryption utility.it calculates the hash algorithm for the plain text.
  • It compares the hash at the time of login of user to give access.
  • The biggest advantage of this algorithm is ,we cannot decrypt the password to plain text only hash is compared of 2 passwords.
  • Add a folder named Utilities in the solution explorer.
  • Create a class named Utility.Import namespace using BCrypt.Net; to access methods under Bcrypt namespace.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using BCrypt.Net;
  7. namespace AuthenticationAngularMVC.Utilities
  8. {
  9. public static class Utility
  10. {
  11. public static string Encryptpassword(string password)
  12. {
  13. string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password, BCrypt.Net.BCrypt.GenerateSalt(12));
  14. return hashedPassword;
  15. }
  16. public static bool CheckPassword(string enteredPassword, string hashedPassword)
  17. {
  18. bool pwdHash = BCrypt.Net.BCrypt.Verify(enteredPassword, hashedPassword);
  19. return pwdHash;
  20. }
  21. }
  22. }
  • Here first method is used to encrypt password and second method is used to compare the password at the time of login.
  • In first method plain text password is passed to the method.
  • In second method plain text password and hashed password from database is passed for comparison.

Start with Controller code

  • Go to the controller folder and create RegisterController in the folder.
  • Replace the Index ActionResult with Register.
  • Create CheckUser method with username as parameter to check the existence of user.
  • Create method for AddUser with Login class as parameter.
  • Here first we check if the data is present in the usr instance or not.
  • Then check if user is already present or not by CheckUser method.
  • Then create object of Logins class to store the data in fields.
  • Encrypt the password by using utility class and passing entered password in the utility class.
  • Utitliy class is a static class and it can be directly accessed without creating the object.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using AuthenticationAngularMvc.Models;
  7. namespace AuthenticationAngularMvc.Controllers
  8. {
  9. public class RegisterController : Controller
  10. {
  11. public ActionResult Register()
  12. {
  13. return View();
  14. }
  15. //To check that user entered is already present or not.
  16. public bool CheckUser(string user)
  17. {
  18. bool Exists = false;
  19. using (SahilEntities context = new SahilEntities())
  20. {
  21. var uName = context.Logins1.Where(x => x.UserName == user).ToList();
  22. if (uName.Count != 0)
  23. {
  24. Exists = true;
  25. }
  26. }
  27. return Exists;
  28. }
  29. //For saving the user details in database table.
  30. public string AddUser(Login1 usr)
  31. {
  32. if (usr != null)
  33. {
  34. if (CheckUser(usr.UserName) == false)
  35. {
  36. using (SahilEntities context = new SahilEntities())
  37. {
  38. Login1 createUser = new Login1();
  39. createUser.UserName = usr.UserName;
  40. createUser.Fname = usr.Fname;
  41. createUser.Lname = usr.Lname;
  42. createUser.Email = usr.Email;
  43. createUser.DateTimeCreated = DateTime.Now;
  44. createUser.Password = Utility.Encryptpassword(usr.Password);
  45. context.Logins1.Add(createUser);
  46. context.SaveChanges();
  47. }
  48. return "User created !";
  49. }
  50. else
  51. {
  52. return "User already present !";
  53. }
  54. }
  55. else
  56. {
  57. return "Invalid Data !";
  58. }
  59. }
  60. }
  61. }
Change path of default controller
  • Find RouteConfig.cs in App_Start folder and change the default Controller and Action.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. namespace AuthenticationAngularMvc
  8. {
  9. public class RouteConfig
  10. {
  11. public static void RegisterRoutes(RouteCollection routes)
  12. {
  13. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  14. routes.MapRoute(
  15. name: "Default",
  16. url: "{controller}/{action}/{id}",
  17. defaults: new { controller = "Register", action = "Register", id = UrlParameter.Optional }
  18. );
  19. }
  20. }
  21. }
Start with AngularJS code
  • Add Module.js,Controller.js,Service.js in content folder.
  • In Module.js start writing code.
  1. var app = angular.module("myApp", []);
  • app.Controller registers the angular controller with name myCntrl.
  • $scope is used to refers the application.Data passed in the $scope is accessible in view.
  • myService used to access the methods in the Service.js file.
  • In SaveUser function, Object User is used to store entered inputs by user.
  • Then response is used to store the response returned by AddUser method in the service.
  • At last message should be displayed on the screen according to the data returned from the service.


  1. app.controller("myCntrl", function ($scope, myService) {
  2. $scope.SaveUser = function () {
  3. $("#divLoading").show();
  4. var User = {
  5. FName: $scope.fName,
  6. LName: $scope.lName,
  7. Email: $scope.uEmail,
  8. Password: $scope.uPwd,
  9. UserName: $scope.uName
  10. };
  11. var response = myService.AddUser(User);
  12. response.then(function (data) {
  13. if (data.data == "1") {
  14. $("#divLoading").hide();
  15. clearFields();
  16. alert("User Created !");
  17. window.location.href = "/Register/Login";
  18. }
  19. else if (data.data == "-1") {
  20. $("#divLoading").hide();
  21. alert("user alraedy present !");
  22. }
  23. else {
  24. $("#divLoading").hide();
  25. clearFields();
  26. alert("Invalid data entered !");
  27. }
  28. });
  29. }
  30. function clearFields() {
  31. $scope.fName = "";
  32. $scope.lName = "";
  33. $scope.Email = "";
  34. $scope.Password = "";
  35. $scope.UserName = "";
  36. }
  37. });
  • In Service.js app.Service is used to register the service with the application.
  • $http is used to call the server methods by providing url ,method,and data.
  • Returned response is then passed from where the function is called.


  1. app.Service("myService", function ($http) {
  2. this.AddUser = function (User) {
  3. var response = $http({
  4. method: "post",
  5. url: "/Register/AddUser",
  6. data: JSON.stringify(User),
  7. dataType: "json"
  8. });
  9. return response;
  10. }
  11. });
Add view for Registration
  • Go to the RegisterController ,right click on the Register Action, select AddView option.


  • Start writing code in RegisterView.


  1. @{
  2. ViewBag.Title = "Register";
  3. }
  4. <html ng-app="myApp">
  5. <head>
  6. <title>Register</title>
  7. <script src="~/Content/Module.js"></script>
  8. <script src="~/Content/Service.js"></script>
  9. <script src="~/Content/Controller.js"></script>
  10. </head>
  11. <body>
  12. <div class="container" ng-controller="myCntrl">
  13. <br />
  14. <div class="row">
  15. <img src="~/Content/Images/user.png" /><h4>Register User</h4>
  16. <hr />
  17. <br />
  18. <div class="col-md-6">
  19. <form name="userForm" novalidate>
  20. <div class="form-horizontal">
  21. <div class="form-group">
  22. <div class="row">
  23. <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
  24. First Name :
  25. </div>
  26. <div class="col-md-6">
  27. <input type="text" class="form-control" placeholder="First Name" name="fName" ng-model="fName" required autofocus />
  28. </div>
  29. </div>
  30. </div>
  31. <div class="form-group">
  32. <div class="row">
  33. <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
  34. Last Name :
  35. </div>
  36. <div class="col-md-6">
  37. <input type="text" class="form-control" placeholder="Last Name" name="lName" ng-model="lName" required autofocus />
  38. </div>
  39. </div>
  40. </div>
  41. <div class="form-group">
  42. <div class="row">
  43. <div class="col-md-3" style="margin-left: 15px; color: #5bc0de">
  44. Email :
  45. </div>
  46. <div class="col-md-6">
  47. <input type="email" class="form-control" placeholder="User's Email" name="uEmail" ng-model="uEmail" required autofocus />
  48. </div>
  49. </div>
  50. </div>
  51. <div class="form-group">
  52. <div class="row">
  53. <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
  54. Username :
  55. </div>
  56. <div class="col-md-6">
  57. <input type="text" class="form-control" placeholder="Username" name="uName" ng-model="uName" required autofocus />
  58. </div>
  59. </div>
  60. </div>
  61. <div class="form-group">
  62. <div class="row">
  63. <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
  64. Password :
  65. </div>
  66. <div class="col-md-6">
  67. <input type="password" class="form-control" placeholder="Password" name="uPwd" ng-model="uPwd" required autofocus />
  68. </div>
  69. </div>
  70. </div>
  71. <div class="form-group">
  72. <div class="row">
  73. <div class="col-md-4"></div>
  74. <div class="col-md-3">
  75. <input type="button" value="Save" ng-click="SaveUser();" class="btn btn-success" />
  76. </div>
  77. <div class="col-md-3">
  78. @Html.ActionLink("Sign in", "Login", "Register", new {@class="btn btn-info" })
  79. </div>
  80. </div>
  81. </div>
  82. <div class="form-group">
  83. <div class="row">
  84. <div class="col-md-6">
  85. <div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px; top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001; opacity: .8; filter: alpha(opacity=70); display: none">
  86. <p style="position: absolute; top: 30%; left: 45%; color: White;">
  87. please wait...<img src="~/Content/images/load.png">
  88. </p>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </form>
  95. </div>
  96. </div>
  97. </div>
  98. </body>
  99. </html>
  • As we discussed earlier ,ng-app is a directive that is used to initialize the app with this module.
  • Drag and drop script files in head tag.
  • ng-controller is used with the div tag to initialize with the controller.
  • In the form write code for all the fields.
  • On the Save click call the method SaveUser in controller.js.


Change the layout of project
  • Find _Layout.cshtml file in the shared folder under views folder.
  • Write code as given below.


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>@ViewBag.Title</title>
  6. <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
  7. <meta name="viewport" content="width=device-width" />
  8. <script src="~/Scripts/angular1.2.18.min.js"></script>
  9. @Styles.Render("~/css")
  10. </head>
  11. <body>
  12. <div class="navbar navbar-default navbar-fixed-top">
  13. <div class="container">
  14. <div class="navbar-header">
  15. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  16. <span class="icon-bar"></span>
  17. <span class="icon-bar"></span>
  18. <span class="icon-bar"></span>
  19. </button>
  20. <a class="navbar-brand" href="#">User Management system</a>
  21. </div>
  22. </div>
  23. </div>
  24. <div id="body">
  25. @RenderSection("featured", required: false)
  26. <section class="content-wrapper main-content clear-fix">
  27. @RenderBody()
  28. </section>
  29. </div>
  30. @Scripts.Render("~/js")
  31. @RenderSection("scripts", required: false)
  32. </body>
  33. </html>
Start your own app