Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Register new user using asp.net core identity

Suggested Videos
Part 63 - Logging to file in asp.net core using nlog | Text | Slides
Part 64 - ASP.NET Core LogLevel configuration | Text | Slides
Part 65 - ASP.NET Core Identity tutorial from scratch | Text | Slides


New User Registration view should look as shown below. To be able to register as a new user we need an email address and password.

register new user using asp.net core identity


RegisterViewModel Class

We will use this RegisterViewModel Class as the model for Register view. It carries the information from the view to the controller class. For validation we are using several asp.net core validation attributes. We discussed these validation attributes and model validation in detail in Parts 42 and 43 of ASP.NET Core tutorial.

using System.ComponentModel.DataAnnotations;

namespace EmployeeManagement.ViewModels
{
    public class RegisterViewModel
    {
        [Required]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password",
            ErrorMessage = "Password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}

AccountController
  • All the user account related CRUD operations will be in this AccountController
  • At the moment we just have the Register action method
  • We reach this action method by issuing a GET request to /account/register
using Microsoft.AspNetCore.Mvc;

namespace EmployeeManagement.Controllers
{
    public class AccountController : Controller
    {
        [HttpGet]
        public IActionResult Register()
        {
            return View();
        }
    }
}

Register View
  • Place this view in Views/Account folder
  • The model for this view is RegisterViewModel which we created above
@model RegisterViewModel

@{
    ViewBag.Title = "User Registration";
}

<h1>User Registration</h1>

<div class="row">
    <div class="col-md-12">
        <form method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Email"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Password"></label>
                <input asp-for="Password" class="form-control" />
                <span asp-validation-for="Password" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ConfirmPassword"></label>
                <input asp-for="ConfirmPassword" class="form-control" />
                <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
            </div>
            <button type="submit" class="btn btn-primary">Register</button>
        </form>
    </div>
</div>

Register Link in the Layout View

Include a navigation menu item in the Layout view to get to the Register view

<div class="collapse navbar-collapse" id="collapsibleNavbar">
    <ul class="navbar-nav">
        <li class="nav-item">
            <a class="nav-link" asp-controller="home" asp-action="index">List</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" asp-controller="home" asp-action="create">Create</a>
        </li>
    </ul>
    <ul class="navbar-nav ml-auto">
        <li class="nav-item">
            <a class="nav-link" asp-controller="account" asp-action="register">
                Register
            </a>
        </li>
    </ul>
</div>

Next Video :
  • Implementing Register action that handles HttpPost to /account/register
  • Create a user account using the posted form data and asp.net core identity
asp.net core tutorial for beginners

2 comments:

  1. Why did we use ViewModel instead of model? Please explain

    ReplyDelete
    Replies
    1. It is Because we need only specific Model to Input data from form

      Delete

It would be great if you can help share these free resources