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.
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.
DI?
ReplyDeleteHi Harry,
DeletePlease 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.
hi check the link
Deletehttp://www.aspdotnet-pools.com/2021/12/login-form-in-aspnet-core-6-using-c_24.html