P.Suresh Kumar

P.Suresh Kumar

  • NA
  • 55
  • 6.4k

Checking form credentials with database in .aspx web forms.

Apr 2 2019 5:11 AM
I have created a login form. Is there any way to evaluate the form credentials with the help of data readily available in database. I have two input boxes; one is the username and the other one is password with a login button attached to it. Whenever the user enters their username and password, they must be definitely clicking on the login button. After clicking it, the credentials must be checked with the sql server database, if available, it must take the user to the next page like "Welcome username" (or) "Login successful" and if not, it must put up an error message like "Not a user, create now". This must be done using .aspx web forms in angularjs.
 
I have also attached my code here.
 
login.css :
  1. .register {  
  2. /* background: -webkit-linear-gradient(left, #1143a6, #00c6ff); */  
  3. margin-top5%;  
  4. padding3%;  
  5. }  
  6. .login-panel {  
  7. width50%;  
  8. min-width300px;  
  9. margin0 auto;  
  10. padding20px;  
  11. background#f8f9fa;  
  12. border-radius: 10px;  
  13. /* border-top-left-radius: 15% 50%; 
  14. border-bottom-left-radius: 15% 50%; */  
  15. }  
  16. .login-panel1 input {  
  17. border1px solid #1143a6;  
  18. border-radius: 1.5rem;  
  19. padding1%;  
  20. width60%;  
  21. background#f8f9fa;  
  22. font-weightbold;  
  23. color#383d41;  
  24. margin-top3%;  
  25. margin-bottom3%;  
  26. cursorpointer;  
  27. }  
  28. .login-heading {  
  29. text-aligncenter;  
  30. color#1143a6;  
  31. padding-bottom10px;  
  32. }  
login.aspx :
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">  
  7. <title>Document</title>  
  8. <link rel="stylesheet" media="screen" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css' />  
  9. <!-- jQuery library -->  
  10. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  11. <!-- Latest compiled JavaScript -->  
  12. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  13. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>  
  14. <link rel="stylesheet" href="login.css">  
  15. <style>  
  16. body {  
  17. background: -webkit-linear-gradient(left, #1143a6, #00c6ff);  
  18. /* background-color: #00b7ff; */  
  19. /* background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='540' height='450' viewBox='0 0 1080 900'%3E%3Cg fill-opacity='.1'%3E%3Cpolygon fill='%23444' points='90 150 0 300 180 300'/%3E%3Cpolygon points='90 150 180 0 0 0'/%3E%3Cpolygon fill='%23AAA' points='270 150 360 0 180 0'/%3E%3Cpolygon fill='%23DDD' points='450 150 360 300 540 300'/%3E%3Cpolygon fill='%23999' points='450 150 540 0 360 0'/%3E%3Cpolygon points='630 150 540 300 720 300'/%3E%3Cpolygon fill='%23DDD' points='630 150 720 0 540 0'/%3E%3Cpolygon fill='%23444' points='810 150 720 300 900 300'/%3E%3Cpolygon fill='%23FFF' points='810 150 900 0 720 0'/%3E%3Cpolygon fill='%23DDD' points='990 150 900 300 1080 300'/%3E%3Cpolygon fill='%23444' points='990 150 1080 0 900 0'/%3E%3Cpolygon fill='%23DDD' points='90 450 0 600 180 600'/%3E%3Cpolygon points='90 450 180 300 0 300'/%3E%3Cpolygon fill='%23666' points='270 450 180 600 360 600'/%3E%3Cpolygon fill='%23AAA' points='270 450 360 300 180 300'/%3E%3Cpolygon fill='%23DDD' points='450 450 360 600 540 600'/%3E%3Cpolygon fill='%23999' points='450 450 540 300 360 300'/%3E%3Cpolygon fill='%23999' points='630 450 540 600 720 600'/%3E%3Cpolygon fill='%23FFF' points='630 450 720 300 540 300'/%3E%3Cpolygon points='810 450 720 600 900 600'/%3E%3Cpolygon fill='%23DDD' points='810 450 900 300 720 300'/%3E%3Cpolygon fill='%23AAA' points='990 450 900 600 1080 600'/%3E%3Cpolygon fill='%23444' points='990 450 1080 300 900 300'/%3E%3Cpolygon fill='%23222' points='90 750 0 900 180 900'/%3E%3Cpolygon points='270 750 180 900 360 900'/%3E%3Cpolygon fill='%23DDD' points='270 750 360 600 180 600'/%3E%3Cpolygon points='450 750 540 600 360 600'/%3E%3Cpolygon points='630 750 540 900 720 900'/%3E%3Cpolygon fill='%23444' points='630 750 720 600 540 600'/%3E%3Cpolygon fill='%23AAA' points='810 750 720 900 900 900'/%3E%3Cpolygon fill='%23666' points='810 750 900 600 720 600'/%3E%3Cpolygon fill='%23999' points='990 750 900 900 1080 900'/%3E%3Cpolygon fill='%23999' points='180 0 90 150 270 150'/%3E%3Cpolygon fill='%23444' points='360 0 270 150 450 150'/%3E%3Cpolygon fill='%23FFF' points='540 0 450 150 630 150'/%3E%3Cpolygon points='900 0 810 150 990 150'/%3E%3Cpolygon fill='%23222' points='0 300 -90 450 90 450'/%3E%3Cpolygon fill='%23FFF' points='0 300 90 150 -90 150'/%3E%3Cpolygon fill='%23FFF' points='180 300 90 450 270 450'/%3E%3Cpolygon fill='%23666' points='180 300 270 150 90 150'/%3E%3Cpolygon fill='%23222' points='360 300 270 450 450 450'/%3E%3Cpolygon fill='%23FFF' points='360 300 450 150 270 150'/%3E%3Cpolygon fill='%23444' points='540 300 450 450 630 450'/%3E%3Cpolygon fill='%23222' points='540 300 630 150 450 150'/%3E%3Cpolygon fill='%23AAA' points='720 300 630 450 810 450'/%3E%3Cpolygon fill='%23666' points='720 300 810 150 630 150'/%3E%3Cpolygon fill='%23FFF' points='900 300 810 450 990 450'/%3E%3Cpolygon fill='%23999' points='900 300 990 150 810 150'/%3E%3Cpolygon points='0 600 -90 750 90 750'/%3E%3Cpolygon fill='%23666' points='0 600 90 450 -90 450'/%3E%3Cpolygon fill='%23AAA' points='180 600 90 750 270 750'/%3E%3Cpolygon fill='%23444' points='180 600 270 450 90 450'/%3E%3Cpolygon fill='%23444' points='360 600 270 750 450 750'/%3E%3Cpolygon fill='%23999' points='360 600 450 450 270 450'/%3E%3Cpolygon fill='%23666' points='540 600 630 450 450 450'/%3E%3Cpolygon fill='%23222' points='720 600 630 750 810 750'/%3E%3Cpolygon fill='%23FFF' points='900 600 810 750 990 750'/%3E%3Cpolygon fill='%23222' points='900 600 990 450 810 450'/%3E%3Cpolygon fill='%23DDD' points='0 900 90 750 -90 750'/%3E%3Cpolygon fill='%23444' points='180 900 270 750 90 750'/%3E%3Cpolygon fill='%23FFF' points='360 900 450 750 270 750'/%3E%3Cpolygon fill='%23AAA' points='540 900 630 750 450 750'/%3E%3Cpolygon fill='%23FFF' points='720 900 810 750 630 750'/%3E%3Cpolygon fill='%23222' points='900 900 990 750 810 750'/%3E%3Cpolygon fill='%23222' points='1080 300 990 450 1170 450'/%3E%3Cpolygon fill='%23FFF' points='1080 300 1170 150 990 150'/%3E%3Cpolygon points='1080 600 990 750 1170 750'/%3E%3Cpolygon fill='%23666' points='1080 600 1170 450 990 450'/%3E%3Cpolygon fill='%23DDD' points='1080 900 1170 750 990 750'/%3E%3C/g%3E%3C/svg%3E"); */  
  20. /* background-color: #ff7700; */  
  21. /* background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1600 900'%3E%3Cpolygon fill='%23cc0000' points='957 450 539 900 1396 900'/%3E%3Cpolygon fill='%23aa0000' points='957 450 872.9 900 1396 900'/%3E%3Cpolygon fill='%23d6002b' points='-60 900 398 662 816 900'/%3E%3Cpolygon fill='%23b10022' points='337 900 398 662 816 900'/%3E%3Cpolygon fill='%23d9004b' points='1203 546 1552 900 876 900'/%3E%3Cpolygon fill='%23b2003d' points='1203 546 1552 900 1162 900'/%3E%3Cpolygon fill='%23d3006c' points='641 695 886 900 367 900'/%3E%3Cpolygon fill='%23ac0057' points='587 900 641 695 886 900'/%3E%3Cpolygon fill='%23c4008c' points='1710 900 1401 632 1096 900'/%3E%3Cpolygon fill='%239e0071' points='1710 900 1401 632 1365 900'/%3E%3Cpolygon fill='%23aa00aa' points='1210 900 971 687 725 900'/%3E%3Cpolygon fill='%23880088' points='943 900 1210 900 971 687'/%3E%3C/svg%3E"); 
  22. background-attachment: fixed; 
  23. background-size: cover; */  
  24. }  
  25. /* .nav-header{ 
  26. background-color:#02A4F3; 
  27. background: linear-gradient(to right, #0bc3c3 0%, #009cff 100%) !important; 
  28. height:60px; 
  29. } 
  30. .nav-header2{ 
  31. background-color:#f1f1f1; 
  32. height:106px; 
  33. } 
  34. .main_cont{ margin:0 auto; 
  35. width:80%; 
  36. height:300px; 
  37. background-color:#f1f1f1; 
  38. margin-top:10px;} 
  39. .box_process{ padding:10px; 
  40. margin:10px; 
  41. border:2px solid #ddd;} */  
  42. </style>  
  43. <script>  
  44. var LoginApp = angular.module('LoginApp', []);  
  45. LoginApp.controller('LoginController', ['$scope''$http''$window'function ($scope, $http, $window) {  
  46. $scope.submit = function () {  
  47. var UserData = { username: $scope.Username, password: $scope.Password }  
  48. $http.post('login.aspx/ValidateUser', UserData)  
  49. .success(function (data, status, headers, config) {  
  50. if (data.d != "") {  
  51. $scope.message = data.d;  
  52. else {  
  53. $window.location.href = 'success.aspx';  
  54. }  
  55. }).error(function (data, status, headers, config) {  
  56. $scope.message = data.d;  
  57. });  
  58. }  
  59. }]);  
  60. </script>  
  61. </head>  
  62. <body>  
  63. <section class="container ">  
  64. <div class="main_cont register">  
  65. <div class="login-panel">  
  66. <h3 class="login-heading"></h3>  
  67. <div class="row">  
  68. <div class="col-md-12 col-sm-12">  
  69. <div class="form-group">  
  70. <input type="text" class="form-control" ng-model="Username" name="Name" placeholder="Username" value="" autocomplete="off" required/>  
  71. </div>  
  72. <div class="form-group">  
  73. <input type="password" class="form-control" ng-model="Password" name="Password" placeholder="Password" value="" autocomplete="off" required/>  
  74. </div>  
  75. <div class="float-right">  
  76. <input type="submit" class="btn btn-primary" ng-click="submit()" value="Login" />  
  77. </div>  
  78. </div>  
  79. </div>  
  80. </div>  
  81. </div>  
  82. </section>  
  83. </body>  
  84. </html>  
login.aspx.cs :
  1. using Newtonsoft.Json;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.Services;  
  9. using System.Web.UI;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.Security;  
  12. using System.Configuration;  
  13. public partial class login : System.Web.UI.Page  
  14. {  
  15. protected void Page_Load(object sender, EventArgs e)  
  16. {  
  17. }  
  18. [WebMethod]  
  19. public static string ValidateUser(string username, string password)  
  20. {  
  21. string status = "";  
  22. int userId = 0;  
  23. string constr = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;  
  24. using (SqlConnection con = new SqlConnection(constr))  
  25. {  
  26. using (SqlCommand cmd = new SqlCommand("Validate_User"))  
  27. {  
  28. cmd.CommandType = CommandType.StoredProcedure;  
  29. cmd.Parameters.AddWithValue("@Username", username);  
  30. cmd.Parameters.AddWithValue("@Password", password);  
  31. cmd.Connection = con;  
  32. con.Open();  
  33. userId = Convert.ToInt32(cmd.ExecuteScalar());  
  34. con.Close();  
  35. }  
  36. switch (userId)  
  37. {  
  38. case -1:  
  39. status = "Username and/or password is incorrect.";  
  40. break;  
  41. case -2:  
  42. status = "Account has not been activated.";  
  43. break;  
  44. default:  
  45. break;  
  46. }  
  47. }  
  48. return status;  
  49. }  
  50. }  
success.aspx :
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="success.aspx.cs" Inherits="View_success" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  6. <link rel="stylesheet" media="screen" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css' />  
  7. <link rel="stylesheet" href="login.css" />  
  8. <title></title>  
  9. </head>  
  10. <body>  
  11. <form id="form1" runat="server">  
  12. <div>  
  13. <h2>Login successful!</h2>  
  14. </div>  
  15. </form>  
  16. </body>  
  17. </html>  
I don't know how to check the details with the database and process it to the next page.
 
Regards
Suresh Kumar P

Answers (5)