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;
- }
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Admin | Log in</title>
-
- <!-- Google Font: Source Sans Pro -->
- <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
- <!-- Font Awesome -->
- <link rel="stylesheet" href="~/plugins/fontawesome-free/css/all.min.css">
- <!-- icheck bootstrap -->
- <link rel="stylesheet" href="~/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
- <!-- Theme style -->
- <link rel="stylesheet" href="~/dist/css/adminlte.min.css">
- </head>
- <body class="hold-transition login-page">
- <div class="login-box">
- <div class="login-logo">
- <a href=""><b>Admin Login</b></a>
- </div>
- <!-- /.login-logo -->
- <div class="card">
- <div class="card-body login-card-body">
- <p class="login-box-msg">Sign in to start your session</p>
- <form method="post">
- <div asp-validation-summary="All" class="text-danger"></div>
- <div class="input-group mb-3">
- <input asp-for="email" class="form-control" placeholder="Email" required>
- <span asp-validation-for="email" class="text-danger"></span>
- <div class="input-group-append">
- <div class="input-group-text">
- <span class="fas fa-envelope"></span>
- </div>
- </div>
- </div>
- <div class="input-group mb-3">
- <input asp-for="password" class="form-control" placeholder="Password" required>
- <span asp-validation-for="password" class="text-danger"></span>
- <div class="input-group-append">
- <div class="input-group-text">
- <span class="fas fa-lock"></span>
- </div>
- </div>
- </div>
- <div class="row">
- <div class="col-8">
- <div class="icheck-primary">
- <input type="checkbox" id="remember">
- <label for="remember">
- Remember Me
- </label>
- </div>
- </div>
- <!-- /.col -->
- <div class="col-4">
- <button type="Submit" class="btn btn-primary btn-block">Sign In</button>
- </div>
- <!-- /.col -->
- </div>
- </form>
-
- <div class="social-auth-links text-center mb-3">
- <p>- OR -</p>
- <a href="#" class="btn btn-block btn-primary">
- <i class="fab fa-facebook mr-2"></i> Sign in using Facebook
- </a>
- <a href="#" class="btn btn-block btn-danger">
- <i class="fab fa-google-plus mr-2"></i> Sign in using Google+
- </a>
- </div>
- <!-- /.social-auth-links -->
-
- <p class="mb-1">
- <a href="forgot-password.html">I forgot my password</a>
- </p>
- <p class="mb-0">
- <a href="" class="text-center">Register a new membership</a>
- </p>
- </div>
- <!-- /.login-card-body -->
- </div>
- </div>
- <!-- /.login-box -->
- <!-- jQuery -->
- <script src="~/plugins/jquery/jquery.min.js"></script>
- <!-- Bootstrap 4 -->
- <script src="~/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
- <!-- AdminLTE App -->
- <script src="~/dist/js/adminlte.min.js"></script>
- </body>
- </html>
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)
- {
-
- return RedirectToAction("Index", "Home");
- }
- else
- {
-
- 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
-
-
-
-
-
-
-
- CREATE PROCEDURE [dbo].[spLoginUser]
-
- @user_name nvarchar(50)=null,
- @password nvarchar(50)=null
- AS
- BEGIN
-
-
- SET NOCOUNT ON;
-
-
- Select * From tbl_login Where user_name=@user_name And password=@password
- END
any help could be appriciate.