In this
article I will show you how you can create a registration and login form in
asp.net core 6 using entity framework in c#.net. In this article I have used Ms
SQL server to store the data and validate the database.
In my
previous article I have already shown how you can connect your asp.net core webapplication from ms sql server database using entity framework and I have
explained where to get the connection string in asp.net core 6 application.
So for this
article first I have crated a table in which we will save the user registration
detail using entity framework in asp.net core 6.
After this
we will run the Scaffold-DbContext to create the DB context. I have already
explained How To Connect MS SQL Server Database in Asp.net Core MVC UsingC#.Net. After creating DB context, we will create a model class.
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 class we will add a new controller and add a IActionresult method
named as registration and login. Please check the code for registration you need
to use in your controller.
[HttpGet] public IActionResult Registration() { LoginModel loginModel = new LoginModel(); return View(loginModel); } [HttpPost]
public IActionResult
Registration(LoginModel loginModel) { try { TestDBContext testDBContext =
new TestDBContext(); var userRegsistration = new UserLoginMaster() { LoginId = loginModel.LoginId, Password =
loginModel.Password };
testDBContext.UserLoginMasters.Add(userRegsistration); testDBContext.SaveChanges(); ViewBag.Message = "User account created successfully"; } catch (Exception ex) { ViewBag.Message = "Error while creating user account"; } return View(loginModel); } |
In above
code I have created HttpGet and HttpPost method. In HttpPost method I have
added the code to save the record in database. Now create a view for
Registration from in you asp.net core application and add the below code.
@model WebApplication1.Models.LoginModel @{
ViewBag.Title = "Registration form"; } @using (Html.BeginForm("Registration", "Home")) {
<span style="font-weight:bolder; font-size:16px; color:red;">@ViewBag.Message</span>
<div> <h2>User
Registration</h2> <div class="row"> <div class="form-group col-6"> <lable for="loginid">Login Id</lable> @Html.TextBoxFor(m => m.LoginId, new { @class = "form-control",
@placeholder = "Enter login id" }) </div> </div> <div class="row"> <div class="form-group col-6"> <lable for="loginid">Password</lable> @Html.PasswordFor(m => m.Password, new { @class = "form-control",
@placeholder = "Enter password" }) </div> </div> <div class="row"> <div class="form-group col-12"> <input type="submit" value="Save" /> </div> </div> <div class="row"> <div class="form-group col-12"> <a href="/Home/Index/">Click to
Login</a> </div> </div>
</div> } |
After this run the application to check the output.
Now click on save to save the data in database. Here you an see we are able to get the user added detail at controller end.
Now press F5 to complete the process.
Now check the data base whether data saved in DB table or not.
Here we can see the user detail which we have added are now available in your database table.
Now we will
work on login form for this we will add the below code in your controller.
[HttpGet] public IActionResult Index() { LoginModel loginModel = new LoginModel(); return View(loginModel); } [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); } |
In above
code http post method is used for validating the user login detail. Please
check the view code for login form in asp.net core application.
@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-6"> <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-6"> <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>
<div class="row"> <div class="form-group col-12"> <a href="/Home/Registration">Click
to Register</a> </div>
</div> } |
Now we have
done run the application and check the output.
Download
0 comments:
Please let me know your view