Tri Setia

Tri Setia

  • 1.8k
  • 188
  • 1.4k

Login Form Mvc without EF in VS 2019

Apr 4 2021 5:32 AM
Hi guys,,l'm learn how to create login without EF in VS 2019 MVC. The login Controller can not return RedirectToAction("Index", "Home"); although the username and password has been entered correctly. I want if user entered correct username and password or success login will redirect to AdminDashboard.
 
this is the login.cshtm
  1. @model AdminLTEMvc.Models.LoginViewModel      
  2. @{      
  3.     Layout = null;      
  4. }      
  5. <!DOCTYPE html>      
  6. <html lang="en">      
  7. <head>      
  8.     <meta charset="utf-8">      
  9.     <meta name="viewport" content="width=device-width, initial-scale=1">      
  10.     <title>Admin | Log in</title>      
  11.       
  12.     <!-- Google Font: Source Sans Pro -->      
  13.     <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">      
  14.     <!-- Font Awesome -->      
  15.     <link rel="stylesheet" href="~/plugins/fontawesome-free/css/all.min.css">      
  16.     <!-- icheck bootstrap -->      
  17.     <link rel="stylesheet" href="~/plugins/icheck-bootstrap/icheck-bootstrap.min.css">      
  18.     <!-- Theme style -->      
  19.     <link rel="stylesheet" href="~/dist/css/adminlte.min.css">      
  20. </head>      
  21. <body class="hold-transition login-page">      
  22.     <div class="login-box">      
  23.         <div class="login-logo">      
  24.             <a href=""><b>Admin Login</b></a>      
  25.         </div>      
  26.         <!-- /.login-logo -->      
  27.         <div class="card">      
  28.             <div class="card-body login-card-body">      
  29.                 <p class="login-box-msg">Sign in to start your session</p>      
  30.                 <form method="post">      
  31.                     <div asp-validation-summary="All" class="text-danger"></div>      
  32.                     <div class="input-group mb-3">      
  33.                         <input asp-for="email" class="form-control" placeholder="Email" required>      
  34.                         <span asp-validation-for="email" class="text-danger"></span>      
  35.                         <div class="input-group-append">      
  36.                             <div class="input-group-text">      
  37.                                 <span class="fas fa-envelope"></span>      
  38.                             </div>      
  39.                         </div>      
  40.                     </div>      
  41.                     <div class="input-group mb-3">      
  42.                         <input asp-for="password" class="form-control" placeholder="Password" required>      
  43.                         <span asp-validation-for="password" class="text-danger"></span>      
  44.                         <div class="input-group-append">      
  45.                             <div class="input-group-text">      
  46.                                 <span class="fas fa-lock"></span>      
  47.                             </div>      
  48.                         </div>      
  49.                     </div>      
  50.                     <div class="row">      
  51.                         <div class="col-8">      
  52.                             <div class="icheck-primary">      
  53.                                 <input type="checkbox" id="remember">      
  54.                                 <label for="remember">      
  55.                                     Remember Me      
  56.                                 </label>      
  57.                             </div>      
  58.                         </div>      
  59.                         <!-- /.col -->      
  60.                         <div class="col-4">      
  61.                             <button type="Submit" class="btn btn-primary btn-block">Sign In</button>      
  62.                         </div>      
  63.                         <!-- /.col -->      
  64.                     </div>      
  65.                 </form>      
  66.       
  67.                 <div class="social-auth-links text-center mb-3">      
  68.                     <p>- OR -</p>      
  69.                     <a href="#" class="btn btn-block btn-primary">      
  70.                         <i class="fab fa-facebook mr-2"></i> Sign in using Facebook      
  71.                     </a>      
  72.                     <a href="#" class="btn btn-block btn-danger">      
  73.                         <i class="fab fa-google-plus mr-2"></i> Sign in using Google+      
  74.                     </a>      
  75.                 </div>      
  76.                 <!-- /.social-auth-links -->      
  77.       
  78.                 <p class="mb-1">      
  79.                     <a href="forgot-password.html">I forgot my password</a>      
  80.                 </p>      
  81.                 <p class="mb-0">      
  82.                     <a href="" class="text-center">Register a new membership</a>      
  83.                 </p>      
  84.             </div>      
  85.             <!-- /.login-card-body -->      
  86.         </div>      
  87.     </div>      
  88.     <!-- /.login-box -->      
  89.     <!-- jQuery -->      
  90.     <script src="~/plugins/jquery/jquery.min.js"></script>      
  91.     <!-- Bootstrap 4 -->      
  92.     <script src="~/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>      
  93.     <!-- AdminLTE App -->      
  94.     <script src="~/dist/js/adminlte.min.js"></script>      
  95. </body>      
  96. </html>  
this is the AccountController.cs
  1. using Microsoft.AspNetCore.Mvc;    
  2. using Microsoft.Extensions.Configuration;    
  3. using System;    
  4. using System.Collections.Generic;    
  5. using System.Linq;    
  6. using System.Threading.Tasks;    
  7. using AdminLTEMvc.Models;    
  8. using Microsoft.Data.SqlClient;    
  9. using System.Data;    
  10.     
  11. namespace AdminLTEMvc.Controllers    
  12. {    
  13.     public class AccountController : Controller    
  14.     {    
  15.         private readonly IConfiguration _configuration;    
  16.         public AccountController(IConfiguration configuration)    
  17.         {    
  18.             this._configuration = configuration;    
  19.         }    
  20.         [HttpGet]    
  21.         public IActionResult Login()    
  22.         {    
  23.             return View();    
  24.         }    
  25.         [HttpPost]    
  26.         [ValidateAntiForgeryToken]    
  27.         public IActionResult Login(LoginViewModel loginViewModel)    
  28.         {    
  29.             try    
  30.             {    
  31.                 using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))    
  32.                 {    
  33.                     con.Open();    
  34.                     SqlCommand cmd = new SqlCommand("spLoginUser", con);    
  35.                     cmd.Parameters.AddWithValue("@user_name", loginViewModel.user_name);    
  36.                     cmd.Parameters.AddWithValue("@password", loginViewModel.password);    
  37.                     cmd.CommandType = CommandType.StoredProcedure;    
  38.                     SqlDataReader rdr = cmd.ExecuteReader();    
  39.                     if (rdr.Read()==true)    
  40.                     {    
  41.                         //if login success    
  42.                         return RedirectToAction("Index""Home");    
  43.                     }    
  44.                     else    
  45.                     {    
  46.                         //if login failed    
  47.                         return RedirectToAction("Login""Account");    
  48.                     }    
  49.                 }    
  50.             }    
  51.             catch(Exception)    
  52.             {    
  53.                 throw;    
  54.             }    
  55.         }    
  56.     }    
  57. }  
this the LoginViewModel.cs
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel.DataAnnotations;    
  4. using System.Linq;    
  5. using System.Threading.Tasks;    
  6.     
  7. namespace AdminLTEMvc.Models    
  8. {    
  9.     public class LoginViewModel    
  10.     {    
  11.         [Key]    
  12.         public int user_id { getset; }    
  13.         [Required]    
  14.         public string user_name { getset; }    
  15.         [Required]    
  16.         [DataType(DataType.Password)]    
  17.         [StringLength(150, MinimumLength = 6)]    
  18.         [Display(Name = "Password: ")]    
  19.         public string password { getset; }    
  20.         [Required]    
  21.         public string email { getset; }    
  22.         public string create_date { getset; }    
  23.         [Required]    
  24.         public string last_login_date { getset; }    
  25.     }    
  26. }  
this is the connection string
  1. {    
  2.   "Logging": {    
  3.     "LogLevel": {    
  4.       "Default""Information",    
  5.       "Microsoft""Warning",    
  6.       "Microsoft.Hosting.Lifetime""Information"    
  7.     }    
  8.   },    
  9.   "AllowedHosts""*",    
  10.   "ConnectionStrings": {    
  11.     "db_mvc""Data Source=.;Initial Catalog=db_lma;Integrated Security=True"    
  12.   }    
  13. }  
this the Store Prosedure
  1. set ANSI_NULLS ON    
  2. set QUOTED_IDENTIFIER ON    
  3. go    
  4.     
  5.     
  6. -- =============================================    
  7. -- Author:      <Author,,Name>    
  8. -- Create date: <Create Date,,>    
  9. -- Description: <Description,,>    
  10. -- =============================================    
  11. CREATE PROCEDURE [dbo].[spLoginUser]    
  12.     -- Add the parameters for the stored procedure here    
  13.     @user_name nvarchar(50)=null,    
  14.     @password nvarchar(50)=null    
  15. AS    
  16. BEGIN    
  17.     -- SET NOCOUNT ON added to prevent extra result sets from    
  18.     -- interfering with SELECT statements.    
  19.     SET NOCOUNT ON;    
  20.     
  21.     -- Insert statements for procedure here    
  22.     Select * From tbl_login Where user_name=@user_name And password=@password    
  23. END  
any help could be appriciate.

Answers (14)