In this article I will show you how you can create a login page in asp.net core mvc / asp.net core 6 web application from ms sql database using c#.net.
- Connect To My Sql Database With Entity Framework Using C# | My SQL And Asp.Net Core 6/7
- Download File From wwwroot Folder Asp.Net Core 6 Using C#
- Asp.Net Core 6: Bind DropDownList and Access Selected Value In Controller Using C#.Net
- Asp.Net Core 6: Upload and Preview Image Using C# In .Net Core (wwwroot)
So first we will create a new asp.net core application. After creating app we will create a new model class file and add the below code.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace EmployeeManager.Models { public class EmpoyeeModel { public string LoginId { get; set; } public string Pasword { get; set; } } } |
Now we will install below mention packages.
Run the below mention command in package manage for entity
frame work DB connection.
Scaffold-DbContext "Server=.\SQLEXPRESS;Database=Employee;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force |
Now at your controller end we will add the below HTTP get code in your controller.
[HttpGet] public IActionResult Index() { EmpoyeeModel
_empoyeeModel = new
EmpoyeeModel(); return View(_empoyeeModel); } |
After this we will create the view and add the below code.
@model EmployeeManager.Models.EmpoyeeModel @{ ViewData["Title"] = "Home Page"; } <div class="container"> <div class="d-flex
justify-content-center h-100"> <div class="card"> <div class="card-header"> <h3>Sign In</h3> <div class="d-flex justify-content-end
social_icon"> <span><i class="fab
fa-facebook-square"></i></span> <span><i class="fab
fa-google-plus-square"></i></span> <span><i class="fab
fa-twitter-square"></i></span> </div> </div> <div class="card-body"> @using (Html.BeginForm("Index", "Home",
FormMethod.Post)) { if (ViewBag.LoginStatus != null) { if (ViewBag.LoginStatus == 0) {
<div class="alert
alert-danger">Invalid login detail</div> } } <div class="input-group
form-group"> <div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span> </div> @Html.TextBoxFor(m
=> m.LoginId, new {
@class = "form-control", @placeholder = "username" }) </div> <div class="input-group
form-group"> <div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span> </div> @Html.PasswordFor(m
=> m.Pasword, new {
@class = "form-control", @placeholder = "password" }) </div> <div class="row align-items-center
remember"> <input type="checkbox">Remember Me </div> <div class="form-group"> <input type="submit" value="Login" class="btn
float-right login_btn"> </div>} </div> <div class="card-footer"> <div class="d-flex
justify-content-center links"> Don't
have an account?<a href="#">Sign Up</a> </div> <div class="d-flex
justify-content-center"> <a href="#">Forgot your password?</a> </div> </div> </div> </div> </div> |
Now we will create code for validating user from DB.
[HttpPost] public IActionResult Index(EmpoyeeModel
_empoyeeModel) {
EmployeeContext _employeeContext = new EmployeeContext(); var status =
_employeeContext.EmployeeLogin.Where(m => m.LoginId ==
_empoyeeModel.LoginId && m.Password == _empoyeeModel.Pasword).FirstOrDefault(); if (status == null) {
ViewBag.LoginStatus = 0; } else { return RedirectToAction("SuccessPage", "Home"); } return View(_empoyeeModel); } |
Now we will create the success action method. So this action
method we are crating to redirect user after successful login.
<h1> Success Page after
login </h1> |
Now check the DB table.
Now run the
application and check the output.
0 comments:
Please let me know your view