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
- @model AdminLTEMvc.Models.LoginViewModel
- @{
- Layout = null;
- }
- "en">
- "utf-8">
- "viewport" content="width=device-width, initial-scale=1">
-
Admin | Log in - "stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
- "stylesheet" href="~/plugins/fontawesome-free/css/all.min.css">
- "stylesheet" href="~/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
- "stylesheet" href="~/dist/css/adminlte.min.css">
- class="hold-transition login-page">
- class="login-box">class="card">class="card-body login-card-body">
class
="login-box-msg">Sign in to start your session- >
"All" class="text-danger">class="input-group mb-3">- for="email" class="form-control" placeholder="Email" required>
- for="email" class="text-danger">
class="input-group-append">class="input-group-text">- class="fas fa-envelope">
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">
- ="remember">
- Remember Me
class="col-4">- class="btn btn-primary btn-block">Sign In
class
="mb-1">- "forgot-password.html">I forgot my password
class
="mb-0">- "" class="text-center">Register a new membership
this is the AccountController.cs
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using AdminLTEMvc.Models;
- using Microsoft.Data.SqlClient;
- using System.Data;
- namespace AdminLTEMvc.Controllers
- {
- public class AccountController : Controller
- {
- private readonly IConfiguration _configuration;
- public AccountController(IConfiguration configuration)
- {
- this._configuration = configuration;
- }
- [HttpGet]
- public IActionResult Login()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public IActionResult Login(LoginViewModel loginViewModel)
- {
- try
- {
- using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("db_mvc")))
- {
- con.Open();
- SqlCommand cmd = new SqlCommand("spLoginUser", con);
- cmd.Parameters.AddWithValue("@user_name", loginViewModel.user_name);
- cmd.Parameters.AddWithValue("@password", loginViewModel.password);
- cmd.CommandType = CommandType.StoredProcedure;
- SqlDataReader rdr = cmd.ExecuteReader();
- if (rdr.Read()==true)
- {
- //if login success
- return RedirectToAction("Index", "Home");
- }
- else
- {
- //if login failed
- return RedirectToAction("Login", "Account");
- }
- }
- }
- catch(Exception)
- {
- throw;
- }
- }
- }
- }
this the LoginViewModel.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Threading.Tasks;
- namespace AdminLTEMvc.Models
- {
- public class LoginViewModel
- {
- [Key]
- public int user_id { get; set; }
- [Required]
- public string user_name { get; set; }
- [Required]
- [DataType(DataType.Password)]
- [StringLength(150, MinimumLength = 6)]
- [Display(Name = "Password: ")]
- public string password { get; set; }
- [Required]
- public string email { get; set; }
- public string create_date { get; set; }
- [Required]
- public string last_login_date { get; set; }
- }
- }
this is the connection string
- {
- "Logging": {
- "LogLevel": {
- "Default": "Information",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*",
- "ConnectionStrings": {
- "db_mvc": "Data Source=.;Initial Catalog=db_lma;Integrated Security=True"
- }
- }
this the Store Prosedure
- set ANSI_NULLS ON
- set QUOTED_IDENTIFIER ON
- go
- -- =============================================
- -- Author:
- -- Create date:
- -- Description:
- -- =============================================
- CREATE PROCEDURE [dbo].[spLoginUser]
- -- Add the parameters for the stored procedure here
- @user_name nvarchar(50)=null,
- @password nvarchar(50)=null
- AS
- BEGIN
- -- SET NOCOUNT ON added to prevent extra result sets from
- -- interfering with SELECT statements.
- SET NOCOUNT ON;
- -- Insert statements for procedure here
- Select * From tbl_login Where user_name=@user_name And password=@password
- END
any help could be appriciate.

Jignesh KumarPosted Apr 5, 2021, 4:26 AM
Tri SetiaPosted Apr 5, 2021, 5:12 AM
@Jignesh Kumar
amazing sir, the code working if use this code block.
Tri SetiaPosted Apr 5, 2021, 3:57 AM
@Jignesh Kumar
Tri SetiaPosted Apr 5, 2021, 3:46 AM
Jignesh KumarPosted Apr 5, 2021, 3:43 AM
Rijwan AnsariPosted Apr 4, 2021, 5:35 PM
Jignesh KumarPosted Apr 4, 2021, 3:33 PM
Tri SetiaPosted Apr 4, 2021, 2:37 PM
Welcome Admin
Jignesh KumarPosted Apr 4, 2021, 1:20 PM
Tri SetiaPosted Apr 4, 2021, 1:00 PM
Tri SetiaPosted Apr 4, 2021, 12:54 PM
Jignesh KumarPosted Apr 4, 2021, 6:37 AM
Satya KarkiPosted Apr 4, 2021, 6:07 AM
Salman BegPosted Apr 4, 2021, 5:45 AM