Hi lingers,
In order to Login, you need to pass the Userid and Password from view to controller and validate with database.
After validation redirect to appropriate view or dislay message to user.
Refer below sample and modify your code accordingly.
SQL
I have used the UserDetails table with the schema as below.
CREATE TABLE UserDetails
(
UserId VARCHAR(50) NOT NULL PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Password VARCHAR(50) NOT NULL
)
INSERT INTO UserDetails VALUES ('1', 'Dharmendra', '12345')
SELECT * FROM UserDetails
Model
LoginDetail
public class LoginDetail
{
public string? UserID { get; set; }
public string? Password { get; set; }
}
UserDetail
public class UserDetail
{
[Key]
public string? UserID { get; set; }
public string? Password { get; set; }
public string? Name { get; set; }
}
DBContext
public class UserDetailDbContext : DbContext
{
public UserDetailDbContext()
{
}
public UserDetailDbContext(DbContextOptions<UserDetailDbContext> options) : base(options)
{
}
public DbSet<UserDetail> UserDetails { get; set; }
}
Repository > IUserDetailManagement
public interface IUserDetailManagement
{
UserDetail GetLogin(string userID, string password);
}
Repository > UserDetailDataManagement
public class UserDetailDataManagement: IUserDetailManagement
{
private UserDetailDbContext _context { get; }
public UserDetailDataManagement(UserDetailDbContext context)
{
this._context = context;
}
public UserDetail GetLogin(string userID, string password)
{
string msg = string.Empty;
UserDetail usr = new UserDetail();
try
{
// Validating User with database.
usr = _context.UserDetails.FirstOrDefault(u => u.UserID == userID && u.Password == password);
}
catch (Exception ex)
{
msg = ex.Message;
}
return usr;
}
}
UserDetailDataManagement
public class UserDetailDataManagement
{
UserDetailDataManagement objBL = new UserDetailDataManagement();
public UserDetail GetLogin(string userID, string password)
{
UserDetail usr = new UserDetail();
try
{
usr = objBL.GetLogin(userID, password);
}
catch (Exception ex)
{
string err = ex.Message;
}
return usr;
}
}
Controller
public class HomeController : Controller
{
private IUserDetailManagement objBL;
public HomeController(IUserDetailManagement _productRepository)
{
this.objBL = _productRepository;
}
public IActionResult Index()
{
return View();
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public IActionResult Login(string userID, string password)
{
if (!ModelState.IsValid)
{
return View();
}
UserDetail erp = objBL.GetLogin(userID, password);
if (erp != null)
{
// Valid User.
return RedirectToAction("Index");
}
else
{
// Invalid UserId Or Password.
ViewBag.Error = "Login failed";
return View();
}
}
}
View
Index
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
<a href="/Home/Login" class="btn btn-primary">Login</a>
</body>
</html>
Login
@model EF_Core_7_MVC.Models.LoginDetail
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
<h4>Login</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Login" asp-controller="Home">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="UserID" class="control-label"></label>
<input asp-for="UserID" class="form-control" />
<span asp-validation-for="UserID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Login" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@if (ViewBag.Error != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.Error");
};
</script>
}
</body>
</html>
Screenshot