How to register new User Using Asp.net core Identity?

Asp.net core Identity Part 1

For registration of the new user, you need to create a ViewModel class for that. We have created RegistrationViewModel class in our application.

public class RegistrationVIewModel
    {

        [Required]
        [EmailAddress]
        public string Email { get; set; }

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


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

In this class, we use a built-in validation attribute. These are present in System.ComponentModel.DataAnnotations namespace and for another EmailAddress attribute we used which is used for the validate the email address.

Similarly, for the password we used Password which is used when we type password on UI then masking is done.

After that, we added the Registration controller and in that we added Register method. After this we add respective View for this Register method.

Below is the code for Controller

public class AccountController : Controller
    {
        public IActionResult Register()
        {
            return View();
        }
    }

Below is the code for View.

@model LearnAspCore.ViewModel.RegistrationVIewModel

@{
    ViewData["Title"] = "Register";
}

<h1>Register</h1>

<h4>RegistrationVIewModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Register" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Email" class="control-label"></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" class="control-label"></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" class="control-label"></label>
                <input asp-for="ConfirmPassword" class="form-control" />
                <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>
How to register new User Using Asp.net core Identity

If you want your method to respond for only get or for only post-call then mark method with [HttpGet] or [HttpPost] call.

http get attribute in asp.net core

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Related posts