Thursday, 23 December 2021

Login Form In Asp.net Core 6 Using C#

12/23/2021 - By Pranav Singh 3

In this article i will show you how you can create a simple login and attractive login form in asp.net core 6 using c#.net.  In this asp,net core 6 project i have validating user id and password.

 So for this project first we will create a new asp.net project. 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". After adding controller add the view. We will create model calss.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

 

namespace WebApplication1.Models

{

    public class LoginModel

    {

        public string LoginId { get; set; }

        public string Password { get; set; }

    }

}

 

After adding model we will create get method. It will look as shown below.


[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"))

{

    <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.LoginId, 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)

        {

            if (loginModel.LoginId == "user" && loginModel.Password == "123456")

            {

                ViewBag.Message = "Success full login";

            }

            else

            {

                ViewBag.Message = "Invalid login detail.";

            }

            return View(loginModel);

        }

 

Now we will check by running the code. Here I have taken a static value. But in your case you validate as per you requirement. If you want to validate from database just validate it from DB. Now we will check whether we will be able to capture the user added value from view to controller end or not.




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



Now if user adding wrong detail, he will get error message.


 

 









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

3 comments:

  1. Replies
    1. Hi Harry,
      Please let me know what is DI? If it means Data Base Integration i am coming up with that article also. This i have just written for knowledge purpose.

      Delete
    2. hi check the link
      http://www.aspdotnet-pools.com/2021/12/login-form-in-aspnet-core-6-using-c_24.html

      Delete

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