Adding more properties in the user table

IdentityUser is the base class, which contains properties such as email, password, and phone number, which are related to the user. When we create the ASP.NET Core application, it creates an empty ApplicationUser class that inherits from the IdentityUser class. In the ApplicationUser class, we can add more properties that will be created once the entity framework migration is run. We will add FirstName, LastName, and MobileNumber properties in our ApplicationUser class, which will be considered when the table is created:

public class ApplicationUser : IdentityUser 
{ 
  public string FirstName { get; set; } 
  public string LastName { get; set; } 
  public string MobileNumber { get; set; } 
} 

Before running the migration, make sure that the DefaultConnection string specified in the ConfigureServices method of the Startup class is valid.

We can run the migration from the Package Manager Console in Visual Studio or through the dotnet CLI toolset. From Visual Studio, select the specific project and run the Add-Migration command, specifying  the migration name, which is Initial in our case:

The preceding command creates the {timestamp}_Initial class file containing the Up and Down methods. The Up method is used to publish the changes in the backend database, whereas the Down method is used to revert the changes done in the database. To apply the changes to the backend database, we will run the Update-Database command, which creates a database that contains AspNet-related tables, which are part of the Identity framework. If you open the AspNetUsers table in design mode, you will see that the custom columns FirstName, LastName, and MobileNumber are there:

We can run the application and create users using the Register option. To protect our APIs, we have to add the Authorize attribute to the Controller or Action level. When the request comes and the user is authenticated, the method will be executed; otherwise, it redirects the request to the Login page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.110.194