0% found this document useful (0 votes)
9 views

Lab 3 - Modify Identity DB on ASP.NET Core

This document provides a detailed guide on modifying the Identity database in an ASP.NET Core application by adding custom user data, updating registration and profile pages, and testing the changes. It includes step-by-step instructions for updating the IdentityUser class, modifying views, and managing user data. The tutorial emphasizes the importance of using the Package Manager Console for database migrations and testing the application functionalities.

Uploaded by

Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 3 - Modify Identity DB on ASP.NET Core

This document provides a detailed guide on modifying the Identity database in an ASP.NET Core application by adding custom user data, updating registration and profile pages, and testing the changes. It includes step-by-step instructions for updating the IdentityUser class, modifying views, and managing user data. The tutorial emphasizes the importance of using the Package Manager Console for database migrations and testing the application functionalities.

Uploaded by

Mark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CT071-3-3-DDAC Modify Identity DB on ASP.

NET Core

Lab 2.2 – Modify Identity DB on ASP.NET Core


Estimated usage time: 1 Hour 30 Minutes

In this topic, you learn how to add custom user data to the Identity DB. You can also learn how to modify
the user registration page and user profile page for your own use.

a. Add custom user data to the Identity DB


(Estimated Total Time Used: 30 minutes)

1. Continued from the Lab 2.1 project.


2. Update the IdentityUser derived class with custom properties. If you named the project
MVC_APU_FlowerShop2023, the file is named
Areas/Identity/Data/MVC_APU_FlowerShopUser2023.cs.

Level 3 Asia Pacific University of Technology & Innovation Page 1 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

3. Update the file with the following code:

[PersonalData]
public string ? CustomerFullName { get; set; }

[PersonalData]
public int CustomerAge { get; set; }

[PersonalData]
public string ? CustomerAddress { get; set; }

[PersonalData]
public DateTime CustomerDOB { get; set; }

Level 3 Asia Pacific University of Technology & Innovation Page 2 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

4. To modify the current Identity database, you must use the Package Manager Console
(PMC).
5. To start the PMC, click on the Tools > NuGet Package Manager > Package Manager
Console.

6. In the Visual Studio Package Manager Console, type the below commands:

Add-Migration CustomUserData

A migration code will be generated after the add-migration command. This migration code
will modify the existing structural schema of the tables to the latest schema.

Level 3 Asia Pacific University of Technology & Innovation Page 3 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

7. After creating a migration file using the add-migration command, you must update the
database. Execute the Update-Database command to create or modify a database schema.

Update-Database

8. Now, in Visual Studio, open the SQL Server Object Explorer (SSOX).

Level 3 Asia Pacific University of Technology & Innovation Page 4 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

9. You will see your database “MVC_APU_FlowerShop2023” located in the SQL Server and
all the default tables are under the database. Now, open the dbo.AspNetUsers, you will see
the new columns appear in the table.

Level 3 Asia Pacific University of Technology & Innovation Page 5 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

b. Update the Account/Register.cshtml page.


(Estimated Total Time Used: 30 minutes)

1. Update the InputModel in Areas/Identity/Pages/Account/Register.cshtml.cs with the following


highlighted code:

[Required(ErrorMessage ="You must enter the name first before


submitting your form!")]
[StringLength(256, ErrorMessage ="You must enter the value between 6
- 256 chars", MinimumLength = 6)]
[Display(Name = "Customer Full Name")] //label
public string customerfullname { get; set; }

[Required]
[Display(Name = "Customer DOB")]
[DataType(DataType.Date)]
public DateTime DoB { get; set; }

Level 3 Asia Pacific University of Technology & Innovation Page 6 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

Level 3 Asia Pacific University of Technology & Innovation Page 7 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

2. Update the OnPostAsync() function in Areas/Identity/Pages/Account/Register.cshtml.cs with the


following highlighted code:

user.CustomerFullName = Input.customerfullname;

user.CustomerDOB = Input.DoB;

user.EmailConfirmed = true;

Level 3 Asia Pacific University of Technology & Innovation Page 8 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

3. Update the Areas/Identity/Pages/Account/Register.cshtml with the following highlighted markup:

<div class=""form-floating mb-3"">


<input asp-for="Input.customerfullname" class="form-control" />
<label asp-for="Input.customerfullname"></label>
<span asp-validation-for="Input.customerfullname" class="text-
danger"></span>
</div>
<div class=""form-floating mb-3"">
<input asp-for="Input.DoB" class="form-control" />
<label asp-for="Input.DoB"></label>
<span asp-validation-for="Input.DoB" class="text-danger"></span>
</div>

Level 3 Asia Pacific University of Technology & Innovation Page 9 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

c. Update the Account/Manage/Index.cshtml page.


(Estimated Total Time Used: 30 minutes)

1. Update the InputModel in Areas/Identity/Pages/Account/Manage/Index.cshtml.cs with the


following highlighted code:

[Required(ErrorMessage = "You must enter the name first before submitting your
form!")]
[StringLength(256, ErrorMessage = "You must enter the value between 6 - 256
chars", MinimumLength = 6)]
[Display(Name = "Your Full Name")] //label
public string customerfullname { get; set; }

[Required]
[Display(Name = "Your DOB")]
[DataType(DataType.Date)]
public DateTime DoB { get; set; }

[Required(ErrorMessage = "You must enter the age first before submitting your
form!")]
[Range(18, 100, ErrorMessage ="You must be 18 years old when register this
member!")]
[Display(Name = "Your Age")] //label
public int age { get; set; }

[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Your Address")]
public string Address { get; set; }

Level 3 Asia Pacific University of Technology & Innovation Page 10 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

Level 3 Asia Pacific University of Technology & Innovation Page 11 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

2. Update the LoadAsync() in Areas/Identity/Pages/Account/Manage/Index.cshtml.cs with the


following highlighted code:

Input = new InputModel


{
PhoneNumber = phoneNumber,
customerfullname = user.CustomerFullName,
age = user.CustomerAge,
Address = user.CustomerAddress,
DoB = user.CustomerDOB
};

Level 3 Asia Pacific University of Technology & Innovation Page 12 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

3. Update the OnPostAsync() in Areas/Identity/Pages/Account/Manage/Index.cshtml.cs with the


following highlighted code:

if(Input.customerfullname != user.CustomerFullName)
{
user.CustomerFullName = Input.customerfullname;
}

if (Input.DoB != user.CustomerDOB)
{
user.CustomerDOB = Input.DoB;
}

if (Input.Address != user.CustomerAddress)
{
user.CustomerAddress = Input.Address;
}

if (Input.age != user.CustomerAge)
{
user.CustomerAge = Input.age;
}
await _userManager.UpdateAsync(user);

Level 3 Asia Pacific University of Technology & Innovation Page 13 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

4. Update the Areas/Identity/Pages/Account/Manage/Index.cshtml with the following


highlighted markup:

<div class="form-floating mb-3">


<input asp-for="Input.customerfullname" class="form-control" />
<label asp-for="Input.customerfullname"></label>
<span asp-validation-for="Input.customerfullname" class="text-danger"></span>
</div>
<div class="form-floating mb-3">
<input asp-for="Input.age" class="form-control" />
<label asp-for="Input.age"></label>
<span asp-validation-for="Input.age" class="text-danger"></span>
</div>
<div class="form-floating mb-3">
<input asp-for="Input.DoB" class="form-control" />
<label asp-for="Input.DoB"></label>
<span asp-validation-for="Input.DoB" class="text-danger"></span>
</div>
<div class="form-floating mb-3">
<input asp-for="Input.Address" class="form-control" />
<label asp-for="Input.Address"></label>
<span asp-validation-for="Input.Address" class="text-danger"></span>
</div>

Level 3 Asia Pacific University of Technology & Innovation Page 14 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

Level 3 Asia Pacific University of Technology & Innovation Page 15 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

d. Test, create, update, delete the custom data in the table.


(Estimated Total Time Used: 20 minutes)

1. Once every above step is done, let’s test the application now.

a. Register a new user

Level 3 Asia Pacific University of Technology & Innovation Page 16 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

b. View the custom user data on the /Identity/Account/Manage page.

c. Update data in User table.

Level 3 Asia Pacific University of Technology & Innovation Page 17 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

d. Select the Personal Data tab.


e. Select the Download button and examined the PersonalData.json file.

f. Test the Delete button, which deletes the logged-on user.

Level 3 Asia Pacific University of Technology & Innovation Page 18 of 19


CT071-3-3-DDAC Modify Identity DB on ASP.NET Core

Summary:

In this tutorial, we learned how to build add, download, and delete custom user data to Identity DB
in an ASP.NET Core project. In the next tutorial, we will learn how to create a simple CRUD
operation in an ASP.NET Core project.

Level 3 Asia Pacific University of Technology & Innovation Page 19 of 19

You might also like