In the previous article , we got an overview of the key concepts related to authentication, authorization and the ASP.NET Core Identity platform. In this post, we’ll go over the entities made available through the platform, and the corresponding relations that will be added to the database once it’s been integrated.
Migrations
To generate the migrations that add the Identity relations, the provided
IdentityDbContext
must first be derived from. For example:
public class ApplicationDbContext: IdentityDbContext {}
We can then generate the migrations via the dotnet ef command:
dotnet ef migrations add IdentityCreate
The migration file should look a little like the one found in this gist
When the table is updated using the generated migration, the following relations are added to the database: AspNetUsers, AspNetRoles, AspNetUserClaims, AspNetUserTokens, AspNetUserLogins, AspNetRoleClaims, andAspNetUserRoles. These relations, and their matching entities, will be summarized in the next section.
Relations
AspNetUsers
- Stores authentication User information in the database
- Represented by the
IdentityUserentity. It’s an implementation ofIdentityUser<TKey>that uses a string as a primary key. - The corresponding columns for the
NormalizedUserNameandNormalizedEmaildefault properties of theIdentityUserare indexed.
AspNetRoles
- Table for the authorization Roles
- Maps to the
IdentityRoleentity. - The corresponding column for the
NormalizedNamedefault property of theIdentityRoleis indexed.
AspNetUserRoles
- The join table for the many-to-many relationship between the
IdentityUserandIdentityRoleentities. EachUsercan have many associatedRoles, and eachRolecan be associated with manyUsers. - Maps to the
IdentityUserRole<TKey>class. - A user’s roles can be obtained via the
GetRolesAsyncmethod of theUserManager.
var user = await userManager.FindByEmailAsync(userDto.Email);
var roles = await userManager.GetRolesAsync(user);
AspNetUserClaims
- Stores the
Claims
that a
Userpossesses. - Represented by the
IdentityUserClaim<TKey>entity. - A user can have many claims.
- A user’s claims can be obtained via the
GetClaimsAsyncmethod of theUserManager.
AspNetUserLogins
- Table for the user’s logins. A login associates the user with a provider.
- Represented by the
IdentityUserLogin<TKey>class. - A user can have many logins.
AspNetUserTokens
- Table for the user’s authentication Tokens .
- Maps to the
IdentityUserToken<TKey>class. - The user’s authentication token can be retrieved via the
GetAuthenticationTokenAsyncmethod of theUserManager.
var authScheme = "google";
var tokenName = "login";
var user = await userManager.FindByEmailAsync(userDto.Email, authScheme, tokenName);
var token = userManager.GetAuthenticationTokenAsync(user);
AspNetRoleClaims
- Table for claims that are granted to all users within a role.
- Maps to the
IdentityRoleClaim<TKey>class.