Sunday, 26 December 2021

Validate User Id and Password From MS SQL Database in Asp.Net Core Using C#

12/26/2021 - By Pranav Singh 0

In this article i will show you how you can validate user id and  password from ms sql server database in asp.net core 6 using c#.net. In this asp,net core 6 project i have validating user id and password from Ms Sql Server Databbase table. So first we will create a table in our Ms Sql DB and add some user detail.


SQL table

Now we need to add the below mention Nuget Package from our package manager.


Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 5.0.13

 

Note: you need to install package as per your need. You can get the Asp.Net Core packages from https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer

After installing the SQL server package. You will get all packages list install in dandies in solution explorer as shown below:


asp.net core decencies


After this we will install Entityframework tool.

asp.net core entityframework tool

Now select the appropriate package version an install it.

After this we will install package to generate entity table object. For this we again need to install the package for entityframework. To perform this action we need to run the below command in package manager.


Scaffold-DbContext "Server=.\SQLExpress;Database=Test;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data

 

Here just check the highlighted name “Data” this is folder where all your entity  table object class will be created. Now check the folder which you have created in root of the project. Check your Data folder for entity table object.



Now in our project we will add model class. 


using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

 

namespace WebApplication1.Models

{

    public class LoginModel

    {

        public string LoginId { getset; }

        public string Password { getset; }

    }

}

 

After creating project we will add controller in your controller folder.  In this asp.net core controller folder we will add a new controller named as "HomeController.cs".


[HttpGet]

        public IActionResult Index()

        {

            LoginModel loginModel = new LoginModel();

            return View(loginModel);

        }

 

Now we will create our view and add the below code.

 

@model WebApplication1.Models.LoginModel

@{

    ViewBag.Title = "Login form";

}

@using (Html.BeginForm("Index", "Home"))

{

    <span style="font-weight:bolder; font-size:16px; color:red;">@ViewBag.Message</span>

    <div>

        <h2>User Login</h2>

        <div class="row">

            <div class="form-group col-12">

                <lable for="loginid">Login Id</lable>

                @Html.TextBoxFor(m => m.LoginId, new { @class = "form-control" })

            </div>

        </div>

        <div class="row">

            <div class="form-group col-12">

                <lable for="loginid">Password</lable>

                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })

            </div>

        </div>

        <div class="row">

            <div class="form-group col-12">

                <input type="submit" value="Submit" />

            </div>

        </div>

    </div>

}

 

In above View code I have imported model class and bind it to the Html helper controls. So for binding the TextBoxFor control I have used lemda expression and bind it will model class property. This will help us to get the user added value in textbox and password control. 


Now we will create the post method and add the below code. This piece of code will help us to get the value from view to controller.

 

  [HttpPost]

        public IActionResult Index(LoginModel loginModel)

        {

            TestDBContext testDBContext = new TestDBContext();

            var status = testDBContext.UserLoginMasters.Where(m => m.LoginId == loginModel.LoginId && m.Password == loginModel.Password).FirstOrDefault();

            if (status != null)

            {

                ViewBag.Message = "Success full login";

            }

            else

            {

                ViewBag.Message = "Invalid login detail.";

            }

            return View(loginModel);

        }

 

Now we will check by running the code. First we will add wrong detail and check the response. In break point we can check that the added value we are getting in model.


login in asp.net core


As we have added a wrong detail. So we will get invalid detail message.

invalid  login


Now here is the final output. As user enter correct detail he will get below success message.


valid login in .net core
Note: Make DB Changes when your use the sample code:

In your case you need to make changes in DB connection string check the code. This piece of code is present in Context file. Which Is there in data folder.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {

            if (!optionsBuilder.IsConfigured)

            {

               optionsBuilder.UseSqlServer("Server=\\SQLEXPRESS;Database=TestDB;Trusted_Connection=True;");

            }

        }

Download (Source Core)

Download(DB Script)

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