Redirecting to next page if and only if user id is related to a single department

Last Reply 2 days ago By dharmendr

Posted 12 days ago

I'm having a table where userid is a primary key. Each and every userid has several records. For example, if user id is 6, it has four employee records belonging to accounts department, each and every record has their own username and password. Other than accounts department, no other department userid shoud not login the page.

AngularHome.js :

var app = angular.module('homeapp', []);
app.controller("HomeController", function ($scope, $http) {
    $scope.btntext = "Login";
    $scope.login = function () {
        if ($scope.loginForm.$valid) {

        } else {
            $scope.submitted = true;
        }
        $scope.btntext = "Please wait..!";
        $http({
            method: "POST",
            url: 'http://stagingserver:85/EBSAjs/Home/userlogin',
            data: $scope.user
        }).success(function (d) {
            $scope.btntext = 'Login';
            if (d == 1) {
                window.location.href = 'http://stagingserver:85/EBSAjs/Home/dashboard';
            }
            else {
                alert(d);
            }
            $scope.user = null;
        }).error(function () {
            alert('failed');
        })
    }
})

AngularDashboard.js :

var app = angular.module("AngulardateApp", []);
app.controller("datectrl", function ($scope) {
    $scope.currentdate = new Date();
});
 

index.cshtml :

@{
    Layout = null;
}
<!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>
    <link href="~/Styles/login.css" rel="stylesheet" />
    <script src="~/script/angular.min.js"></script>
    <script src="~/script/AngularController/Angularhome.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>

user.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AnguarLogin.Models
{
    public class user
    {
        public string username { get; set; }
        public string password { get; set; }
    }
}

db.cs :

using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using AnguarLogin.Models;

namespace AnguarLogin.database_Access_Layer
{
    public class db
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString);

        public int userlogin(user us)
        {
            SqlCommand com = new SqlCommand("Sp_User_login", con);
            com.CommandType = CommandType.StoredProcedure;
            //com.Parameters.AddWithValue("@Userid",us.userid);
            com.Parameters.AddWithValue("@Username",us.username);
            com.Parameters.AddWithValue("@Password",us.password);
            SqlParameter oblogin = new SqlParameter();
            oblogin.ParameterName = "@Isvalid";
            oblogin.Direction = ParameterDirection.Output;
            oblogin.SqlDbType = SqlDbType.Bit;
            com.Parameters.Add(oblogin);
            con.Open();
            com.ExecuteNonQuery();
            int res = Convert.ToInt32(oblogin.Value);
            con.Close();
            return res;
        }
    }
}

HomeController.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnguarLogin.Models;
using System.Data;

namespace AnguarLogin.Controllers
{
    public class HomeController : Controller
    {
        // GET: /Home/
        database_Access_Layer.db dblayer = new database_Access_Layer.db();
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult dashboard()
        {
            //Session["UserId"] = 1;
            return View();
        }

        public JsonResult userlogin(user us)
        {
            string result = Convert.ToString(dblayer.userlogin(us));
            //string result = "1";
            if (result=="1")
            {
                Session["UserName"] = us.username;
            }
            else
            {
                result = "Username and/or Password is incorrect";                
            }
            return Json(result,JsonRequestBehavior.AllowGet);
        }
    }
}

Sp_User_Login :

USE [ERPTESTN]
GO
/****** Object:  StoredProcedure [dbo].[Sp_User_login]    Script Date: 5/10/2019 10:40:06 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--EXEC [Sp_User_login] @Username='palpandi', @password='123',@IsValid=0
ALTER proc [dbo].[Sp_User_login]
@Username varchar(100)=NULL,
@Password nvarchar(20)=NULL,
@Isvalid int out
as
begin
IF EXISTS(SELECT * FROM USERLOGIN WHERE USERNAME=ISNULL(@Username,'') and Password=ISNULL(@Password,''))
BEGIN
--Set @Isvalid=(Select ISNULL(Count(USERNAME),0) from USERLOGIN where USERNAME=ISNULL(@Username,'') and Password=ISNULL(@Password,''))
Set @Isvalid=(Select ISNULL(USERID,0) from USERLOGIN where USERNAME=ISNULL(@Username,'') and Password=ISNULL(@Password,''))
Print @IsValid
End
ELSE
SET @Isvalid=0
END

 

You are viewing reply posted by: dharmendr 7 days ago.
Posted 7 days ago

Please share your table structures and sample data.

And let me know what exactly you want to display.