Skip to content
Loading
      
  •                               
  •                           
  •                     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 { get; set; }    
    13.         [Required]    
    14.         public string user_name { get; set; }    
    15.         [Required]    
    16.         [DataType(DataType.Password)]    
    17.         [StringLength(150, MinimumLength = 6)]    
    18.         [Display(Name = "Password: ")]    
    19.         public string password { get; set; }    
    20.         [Required]    
    21.         public string email { get; set; }    
    22.         public string create_date { get; set; }    
    23.         [Required]    
    24.         public string last_login_date { get; set; }    
    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.
    and this the AdminLayout.cshtml that accessed from Index.cshtml
     
    that show message "Welcome Admin" if the success login.
     
     
    +2
  • Tri Setia
    +2
    @Rijwan Ansari
    I'm has been try like that sir, but the result still same. The code always jump and directly acces block code after else statement.
    1. else  
    2. {  
    3. ViewData["Pesan"] = "User Name atau password salah,,, silahkan ulangi kembali !!!";  
    4. return RedirectToAction("Login", "Account");  
    5. }  
    and this code block always not working.
    1. if (rdr.Read())  
    2. {  
    3. ViewData["Pesan"] = "Berhasil Login";  
    4. con.Close();  
    5. //if success login  
    6. return RedirectToAction("Index", "AdminDashboard");  
    7. }  
    I have do like this too, but the result stil same code directly jumpt and always access in
    1. else  
    2. {  
    3. ViewData["Pesan"] = "User Name atau password salah,,, silahkan ulangi kembali !!!";  
    4. return RedirectToAction("Login", "Account");  
    5. }  
    this block not working sir..
    1. if (rdr.HasRows)  
    2. {  
    3. rdr.Read();  
    4. con.Close();  
    5. ViewData["message"] = "Berhasil Login";  
    6. return RedirectToAction("Index", "AdminDashboard");  
    7. }  
    +2
  • Tri Setia
    +2
    @Jignesh Kumar
    I has been add 
     
    1. cmd.ExecuteNonQuery();   
     but the result still same,, the code directly access in block code after else statement. can not Redirect to return RedirectToAction("Index", "AdminDashboard");
    1. else        
    2.                    {        
    3.                        ViewData["Pesan"] = "User Name atau password salah,,, silahkan ulangi kembali !!!";        
    4. return RedirectToAction("Login", "Account");     
    5.                    }      
     
    +2
  • Tri Setia
    +1
    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