Author : MD TAREQ HASSAN | Updated : 2020/05/28

Whais ASP.NeCore Identity?

Notes:

Identity helper functionalities

ASP.Net core Identity helper functionalities Step 1

ASP.Net core Identity helper functionalities Step 2

Resources

Structure and Architecture

ASP.Net core Identity Structure and Architecture Step 1

ASP.Net core Identity Structure and Architecture Step 2

UserManager<TUser>

public UserManager(IUserStore<TUser> store,
	IOptions<IdentityOptions> optionsAccessor,
	IPasswordHasher<TUser> passwordHasher,
	IEnumerable<IUserValidator<TUser>> userValidators,
	IEnumerable<IPasswordValidator<TUser>> passwordValidators,
	ILookupNormalizer keyNormalizer,
	IdentityErrorDescriber errors,
	IServiceProvider services,
	ILogger<UserManager<TUser>> logger)
{
	// ... ... ...
}

RoleManager<TRole>

public RoleManager(IRoleStore<TRole> store,
	IEnumerable<IRoleValidator<TRole>> roleValidators,
	ILookupNormalizer keyNormalizer,
	IdentityErrorDescriber errors,
	ILogger<RoleManager<TRole>> logger)
{
	// ... ... ...
}

IUserStore<TUser> interface

Task<IdentityResult> CreateAsync(TUser user, CancellationToken token);
Task<IdentityResult> UpdateAsync(TUser user, CancellationToken token);
Task<IdentityResult> DeleteAsync(TUser user, CancellationToken token);
Task<TUser> FindByIdAsync(string userId, CancellationToken token);
Task<TUser> FindByNameAsync(string normalizedUserName, CancellationToken token);
Task<string> GetUserIdAsync(TUser user, CancellationToken token);
Task<string> GetUserNameAsync(TUser user, CancellationToken token);
Task SetUserNameAsync(TUser user, string userName, CancellationToken token);
Task<string> GetNormalizedUserNameAsync(TUser user, CancellationToken token);
Task SetNormalizedUserNameAsync(TUser user, string normalizedName, CancellationToken token)

IUserStore sub-interfaces

We can extend any of these stores and add properties/methods if we need

Identity model

The Identity model consists of the following entity types

Entity type Description
User Represents the user
Role Represents a role
UserClaim Represents a claim that a user possesses
UserToken Represents an authentication token for a user
UserLogin Associates a user with a login
RoleClaim Represents a claim that’s granted to all users within a role
UserRole A join entity that associates users and roles

See:

ASP.Net core identity defines default Common Language Runtime (CLR) types for each of the entity types listed above. These types are all prefixed with Identity:

Identity Packages

Default identity entities

IdentityUser

public class IdentityUser : IdentityUser<string> {
	
	// ... ... ...

	string UserName { get; set; }
	string NormalizedUserName { get; set; }
	string Email { get; set; }
	string NormalizedEmail { get; set; }
	bool EmailConfirmed { get; set; }
	string PasswordHash { get; set; }
	string SecurityStamp { get; set; }
	string PhoneNumber { get; set; }
	bool PhoneNumberConfirmed { get; set; }
	bool TwoFactorEnabled { get; set; }
	DateTime? LockoutEndDateUtc { get; set; }
	bool LockoutEnabled { get; set; }
	int AccessFailedCount { get; set; }
}

IdentityUserClaim

public class IdentityUserClaim<TKey> {
	public int Id { get; set; }
	public string ClaimType { get; set; }
	public string ClaimValue { get; set; }
	public TKey UserId { get; set; }
	public Claim ToClaim() { //... }
}

IdentityUserLogin

public class IdentityUserLogin<TKey> {
	public string LoginProvider { get; set; }
	public string ProviderKey { get; set; }
	public string ProviderDisplayName { get; set; }
	public TKey UserId { get; set; }
}

IdentityUserToken

public class IdentityUserToken<TKey> {
	public string Name { get; set; }
	public string Value { get; set; }
	public string LoginProvider { get; set; }
	public TKey UserId { get; set; }
}

IdentityRole

public class IdentityRole : IdentityRole<string> { //... }

public class IdentityRole<TKey> {
	public TKey Id { get; set; }
	public string Name { get; set; }
	public string NormalizedName { get; set; }
}
public class IdentityRoleClaim<TKey> {
	public int Id { get; set; }
	public string ClaimType { get; set; }
	public string ClaimValue { get; set; }
	public TKey RoleId { get; set; }
	public Claim ToClaim() { //... }
}
public class IdentityUserRole<TKey> {
	public TKey UserId { get; set; }
	public TKey RoleId { get; set; }
}

Exploring default implementation

Scaffolding default implementation

Step-1

ASP.Net core Identity scaffolding default implementation Step 1

Step-2

ASP.Net core Identity scaffolding default implementation Step 2

Step-3

ASP.Net core Identity scaffolding default implementation Step 3

Step-4

ASP.Net core Identity scaffolding default implementation Step 4

Step-5

ASP.Net core Identity scaffolding default implementation Step 5

Step-6

ASP.Net core Identity scaffolding default implementation Step 6

Step-7

ASP.Net core Identity scaffolding default implementation Step 7

Step-8

ASP.Net core Identity scaffolding default implementation Step 8

Step-9

ASP.Net core Identity scaffolding default implementation Step 9

Step-10

ASP.Net core Identity scaffolding default implementation Step 10

See: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity

Migration for identity store

Step-1

ASP.Net core Identity migration for identity store Step 1

Step-2

ASP.Net core Identity migration for identity store Step 2

Step-3

ASP.Net core Identity migration for identity store Step 3

Step-4

ASP.Net core Identity migration for identity store Step 4

Step-5

ASP.Net core Identity migration for identity store Step 5

Step-6

ASP.Net core Identity migration for identity store Step 6

Step-7

ASP.Net core Identity migration for identity store Step 7

Step-8

ASP.Net core Identity migration for identity store Step 8

See: https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/