Skip to content
Loading
Login Form Mvc without EF in VS 2019      
  •                           
  •                     class="input-group mb-3">      
  •                         for="password" class="form-control" placeholder="Password" required>      
  •                         for="password" class="text-danger">      
  •                         class="input-group-append">      
  •                             class="input-group-text">      
  •                                 class="fas fa-lock">      
  •                                   
  •                               
  •                           
  •                     class="row">      
  •                         class="col-8">      
  •                             class="icheck-primary">      
  •                                 "checkbox" id="remember">      
  •                                 for="remember">      
  •                                     Remember Me      
  •                                       
  •                                   
  •                               
  •                               
  •                         class="col-4">      
  •                             "Submit" class="btn btn-primary btn-block">Sign In      
  •                               
  •                               
  •                           
  •                       
  •       
  •                 class="social-auth-links text-center mb-3">      
  •                     

    - OR -

          
  •                     "#" class="btn btn-block btn-primary">      
  •                         class="fab fa-facebook mr-2"> Sign in using Facebook      
  •                           
  •                     "#" class="btn btn-block btn-danger">      
  •                         class="fab fa-google-plus mr-2"> Sign in using Google+      
  •                           
  •                       
  •                       
  •       
  •                 class="mb-1">      
  •                     "forgot-password.html">I forgot my password      
  •                 

          
  •                 class="mb-0">      
  •                     "" class="text-center">Register a new membership      
  •                 

          
  •                   
  •                   
  •               
  •           
  •           
  •           
  •     "~/plugins/jquery/jquery.min.js">      
  •           
  •     "~/plugins/bootstrap/js/bootstrap.bundle.min.js">      
  •           
  •     "~/dist/js/adminlte.min.js">      
  •       
  •   
  • 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:          
    8. -- Create date:     
    9. -- 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.

    14 Replies

    Know the answer? Post it — somebody with the same question will find it here.

      
    and this the AdminLayout.cshtml that accessed from Index.cshtml
     
    that show message "Welcome Admin" if the success login.
     
     
    +2
  • Jignesh Kumar
    • Have debug the code?
    • Are you getting data in your reader ?
     
    As per your lastscreenshot you are redirect to AccountController and Index method. Can you please share AdminDashBoardController here ? so we can help you..
     
    I am suspecting you are not going in Loggin sucessful code and finally its redirecting to AccountController Index method. 
    +2
  • Tri Setia
    @Jignesh Kumar 
    this is 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.CommandType = CommandType.StoredProcedure;    
    36.                     cmd.Parameters.AddWithValue("@user_name", loginViewModel.user_name);    
    37.                     cmd.Parameters.AddWithValue("@password", loginViewModel.password);    
    38.                     SqlDataReader rdr = cmd.ExecuteReader();    
    39.                     if (rdr.Read())    
    40.                     {    
    41.                         ViewData["Pesan"] = "Berhasil Login";  
    42.                         //if success login    
    43.                         return RedirectToAction("Index""AdminDashboard");    
    44.                     }    
    45.                     else    
    46.                     {    
    47.                         ViewData["Pesan"] = "User Name atau password salah,,, silahkan ulangi kembali !!!";    
    48.                     }    
    49.                     con.Close();    
    50.                     //if failed login  
    51.                     return RedirectToAction("Login""Account");    
    52.                 }    
    53.             }    
    54.             catch(Exception)    
    55.             {    
    56.                 throw;    
    57.             }    
    58.         }    
    59.     }    
    60. }    
     what wrong with that AccountController.cs sir ???
    +2
  • Tri Setia
    hi @Jignesh Kumar l has been try it but l got blank page after success login,,
    problem not yet solved. 
      after success login get blank 
     
    +1
  • Jignesh Kumar
    Hi,
     
    You need to redirect to Actionmethod of AdminDashbord controller, i.e.  your controller name "AdminDashboard" and it has Action method like "Dashboard". Please do change according to your controller name and Actionmethodname. 
    1. if (rdr.Read()==true)    
    2. {    
    3.  //if login success    
    4.  return RedirectToAction("Index", "AdminDashboard");    
    5. }    
    6.  else    
    7.    {    
    8.       //if login failed    
    9.       return RedirectToAction("Login""Account");    
    10.    }    
     
    +3
  • Satya Karki
    Hi, according to your requirement, instead of redirecting to Home page you should redirect to Admin Dashboard Index page.
     
    Inplace of this 
    1. //if login success    
    2. return RedirectToAction("Index""Home");    
    redirect it to your Admin dashboard.
    +4
  • Salman Beg
    If I understood your requirement correctly, you want to redirect to dashboard page after login success. So as per your code, replace return RedirectToAction("Index", "Home"); with your particular controller and action method inside RedirectToAction. Thanks
    +3