Friday, 26 August 2022

Asp.Net Core 6: Login Form Using Ms SQL C# | Simple Login Using Entity Framework in Asp.net Core MVC

8/26/2022 - By Pranav Singh 0

 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.

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.

 



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top