How to differ the api uri link for different users?
UsersController.cs :
using WebApi.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Serialization;
namespace WebApi.Controllers
{
public class UsersController : ApiController
{
public static IList<User> listCusDt = new List<User>()
{
};
[AcceptVerbs("GET")]
public User RPCStyleMethodFetchFirstCustomerDetails()
{
return listCusDt.FirstOrDefault();
}
[HttpGet]
[ActionName("GetAllUsers")]
public string Get(string username, string password)
{
SqlDataReader reader = null;
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "Sp_User_Login";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.AddWithValue("@Username", username);
sqlCmd.Parameters.AddWithValue("@Password", password);
myConnection.Open();
reader = sqlCmd.ExecuteReader();
List<User> users = new List<User>();
User u = null;
while (reader.Read())
{
u = new User();
u.userid = Convert.ToInt32(reader.GetValue(0));
u.username = reader.GetValue(1).ToString();
u.empid = Convert.ToInt32(reader.GetValue(2));
u.password = reader.GetValue(3).ToString();
users.Add(u);
}
myConnection.Close();
//var js = new JavaScriptSerializer();
string json = JsonConvert.SerializeObject(users);
return json;
}
}
}
index.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Login Page</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/ngstorage/0.3.6/ngStorage.min.js"></script>
<link href="../Styles/index.css" rel="stylesheet" />
<!--<script src="../Scripts/angular.min.js"></script>-->
<script src="../Scripts/index.js"></script>
<style>
body {
background: -webkit-linear-gradient(left, #1143a6, #00c6ff);
}
</style>
</head>
<body ng-app="homeapp" ng-controller="HomeController">
<section class="container ">
<div class="main_cont register">
<form name="loginForm" novalidate>
<div class="login-panel">
<h3 class="login-heading">Zetek Castings</h3>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<input type="text" ng-class="{ errorinput: submitted && loginForm.username.$invalid }" name="username" class="form-control" placeholder="Username" ng-model="user.username" required autofocus>
<span class="red-text" ng-if="loginForm.username.$error.required && loginForm.username.$dirty">
<br>Username is a required field<br>
</span>
</div>
<div class="form-group">
<input type="password" ng-class="{ errorinput: submitted && loginForm.password.$invalid }" name="password" class="form-control" placeholder="Password" ng-model="user.password" required>
<span class="red-text" ng-if="loginForm.password.$error.required && loginForm.password.$dirty">
<br>Password is a required field<br>
</span>
</div>
<div class="float-right">
<input type="submit" class="btn btn-primary" value="{{btntext}}" ng-click="login()">
</div>
</div>
</div>
</div>
</form>
</div>
</section>
</body>
</html>
index.js :
/// <reference path="../Views/Warehouse/dashboard.html" />
/// <reference path="index.js" />
var app = angular.module('homeapp', ['ngStorage']);
app.controller("HomeController", function ($scope, $sessionStorage, $http, $window) {
$scope.btntext = "Login";
$scope.login = function () {
debugger;
//alert("SUBMIT " + $scope.user.username);
$scope.stat = "false";
//$sessionStorage.stat = $scope.stat;
angular.forEach($scope.mydata, function (item) {
debugger;
if ((item.username == $scope.user.username) && (item.Password == $scope.user.password))
alert("stat2");
stat = "true";
});
$scope.user.username = "";
$scope.user.password = "";
if ((stat == "true")) {
alert("Success");
//$sessionStorage.SessionMessage = $scope.user.username + $scope.user.password;
$window.open('http://localhost:55463/Views/Warehouse/dashboard.html');
//$window.alert($sessionStorage.SessionMessage);
}
else
alert("Failure");
};
$scope.user = {
"username": "",
"password": ""
};
$scope.mydata = {};
//$sessionStorage.mydata = $scope.mydata;
$http.get("http://localhost:60917/api/Users/GetAllUsers")
.then(function (response) {
debugger;
//$scope.mydata = response.data;
$scope.mydata = JSON.parse(response.data);
$sessionStorage.SessionMessage = response.data;
angular.forEach($scope.mydata, function (item) {
//alert(item.empid);
//alert(item.userid);
})
});
});