In this part we would cover user registration, user login and profile. We would cover the following:
- Add Firstname and Lastname Fields
- Re-Add the Migration
- Add Protected Content
- Disable Some Constraints
1. Add Firstname and Lastname Fields
First, we are going to modify the modify the registration page to add fields for Firstname and Lastname. Next we configure and test the user registration and login function.
Step 1 – Open the ApplicationUser.cs field which is inside the Data folder. Modify it to add the following lines:
public class ApplicationUser : IdentityUser { [PersonalData] [Column(TypeName ="nvarchar(50)")] public string FirstName { get; set; } [PersonalData] [Column(TypeName = "nvarchar(50)")] public string LastName { get; set; } }
Step 2 – Open the Register.cshtml.cs file and modify the InputModel to include Firstname and Lastname. The code snipped is given below:
[Required] [DataType(DataType.Text)] [Display(Name = "First name")] public string FirstName { get; set; } [Required] [DataType(DataType.Text)] [Display(Name = "Last name")] public string LastName { get; set; }
Step 1 – Modify the new user line to include Firstname and Lastname inside the onPostAsync function. The line is shown below:
var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email };
Step 4 – Open the Register.cshtml registration form and add two additional field for Firstname and Lastname. You can simply duplicate the fields for email and adjust. The code you’ll add is shown below:
<div class="form-group"> <label asp-for="Input.FirstName"></label> <input asp-for="Input.FirstName" class="form-control" /> <span asp-validation-for="Input.FirstName" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Input.LastName"></label> <input asp-for="Input.LastName" class="form-control" /> <span asp-validation-for="Input.LastName" class="text-danger"></span> </div>
2. Re-Add the Migrations
Step 5 – Delete the AuthData migration.
Step 6 – Also delete the Database because we’ll re-create it
Step 7 – Add again the AuthData migration. You should be able to do this from Part 4.
Step 8 – Update the database using the HospitalContext
Step 9 – Update the database again using the AuthContext
Now you can launch the application and visit the register page. Create a user and see how it all works.
3. Add Protected Content
Now we would like to have the user be logged in before he can access the home page. To do this, follow the steps below:
Step 1 – Open the HomeController.cs file and annotate the class with the [Authorize] annotation.
Now when you launch the application, it would redirect to the Login page. So you can register a user and then log in.
4. Disable Some Constraints (optional)
Once you register, you will notice that you are redirected to an email confirmation page. We would like to disable that since we don’t have email confirmation yet.
We can also disable password strength feature since this is just a demo. To do these, follow the steps below:
Step 1 – Open the IdentityHostingStartup.cs file and adjust the options parameter of the AddDefaultIdentity() like so:
services.AddDefaultIdentity<ApplicationUser>(options => { options.SignIn.RequireConfirmedAccount = true; options.SignIn.RequireConfirmedEmail = false; options.Password.RequireUppercase = false; })
There are much that can be done on authentication and authorization in .Net Core. However, I’m making another complete tutorial that covers ASP.NET Authentication and Authorization. For now, we would just continue the tutorial.