During this session, we are going to create a user in ASP.NET Core with Identity Entity Framework Core. For this purpose, we use UserManager Class from Microsoft.AspNetCore.Identity. In Session 59, we added Microsoft.AspNetCore.Identity.EntityFrameworkCore to our project and added the Identity entities to our project. Now we start the authentication process by creating a user that can log in (authenticate) to our web app.
Create a Controller for User Management
First, we need to create a controller to handle the user management process. also, we create an action method for creating a user, which we call it CreateUser.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class UserManagmentController : Controller { [HttpGet] public IActionResult CreateUser() { return View(); } } |
Also, we need to inject UserManager class into the controller for handling create user process.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class UserManagmentController : Controller { [HttpGet] public IActionResult CreateUser() { return View(); } } |
As you can see, the Action Method was decorated with [HttpGet] and returned a view that we create later. Also, we need to inject UserManager class into the controller for handling create user process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class UserManagmentController : Controller { private readonly UserManager<IdentityUser> userManger; public UserManagmentController(UserManager<IdentityUser> _userManger) { userManger = _userManger; } [HttpGet] public IActionResult CreateUser() { return View(); } } |
Next, we need a ViewModel which transfers data between View and Controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class CreateUserViewModel { [Required] [EmailAddress] public string Email { get; set; } [Required] [DataType(DataType.Password)] public string Password { get; set; } [Required] [DataType(DataType.Password)] [DisplayName("Confirm Password")] [Compare("Password", ErrorMessage = "The Confirm Password Does Not Match With Password")] public string ConfirmPassword { get; set; } } |
The ViewModel has three properties (Email, Password, and ConfirmPassword) that have the required data annotation. If you need to know more about these data annotations, you can watch Session 38, Session 39, and Session 40.
View and Action Method For Creating User
At this stage, we need to create a View which has a form and use CreateUserViewModel ViewModel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@model CreateUserViewModel @{ ViewBag.Title = "Create New User"; } <form asp-controller="UserManagment" asp-action="CreateUser" method="post" class="mt-5 offset-3 col-6 border border-primary pt-2 pb-2 px-2 py-2"> <div> <label asp-for="Email" class="form-label"></label> <input asp-for="Email" class="form-control" /> <span asp-validation-for="Email" class="text-danger"></span> </div> <div> <label asp-for="Password" class="form-label"></label> <input asp-for="Password" class="form-control" /> <span asp-validation-for="Password" class="text-danger"></span> </div> <div> <label asp-for="ConfirmPassword" class="form-label"></label> <input asp-for="ConfirmPassword" class="form-control" /> <span asp-validation-for="ConfirmPassword" class="text-danger"></span> </div> <div><button type="submit" class="btn btn-primary mt-2">Save</button></div> <div asp-validation-summary="All" class="text-danger"></div> </form> @if (ViewBag.RegisteredResult == "Yes") { <div class="alert alert-success mt-2 offset-3 col-6" role="alert"> Your account was successfully registered </div> } @if (ViewBag.RegisteredResult == "No") { <div class="alert alert-danger mt-2 offset-3 col-6" role="alert"> Internal Error: The user has not been created! </div> } |
Then, as you can see the form is submitted with the Post method and we need to create an HTTP POST action method for CreateUser Action Method into UserManagment Controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[HttpPost] public async Task<IActionResult> CreateUser(CreateUserViewModel createUserViewModel) { if (ModelState.IsValid) { IdentityUser user = new IdentityUser { UserName = createUserViewModel.Email, Email = createUserViewModel.Email }; var result = await userManger.CreateAsync(user, createUserViewModel.Password); if (result.Succeeded) { ViewBag.RegisteredResult = "Yes"; ModelState.Clear(); } else { ViewBag.RegisteredResult = "No"; foreach(var error in result.Errors) { ModelState.AddModelError("Validation", error.Description); } } } return View(); } |
As shown above, we use UserManager to create a user which needs an object of IdentityUser. Also, we store the result of the CreateAsync method in an object to check whether the user is created or not.
In the end, we add an item to the main menu which calls CreateUser Action Method of UserManagment Controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav me-auto mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link" aria-current="page" asp-controller="home" asp-action="index">Home</a> </li> <li class="nav-item"> <a class="nav-link" asp-controller="cost" asp-action="index">Cost List</a> </li> <li class="nav-item"> <a class="nav-link" asp-controller="category" asp-action="index">Category List</a> </li> </ul> <ul class="navbar-nav d-flex"> <li class="nav-item"> <a class="nav-link" asp-controller="UserManagment" asp-action="CreateUser">Register User</a> </li> </ul> </div> |
If you need more details, watch this session video. Also, to be updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.
You can download this Session Slides form HERE.