Search code examples
asp.net-coreasp.net-identity

What difference between Microsoft.AspNetCore.Identity.IdentityUser and custom ApplicationUser


I can add services with my own user definition as

Builder.Services.AddIdentity(Of ApplicationUser, ApplcationRole)...

than

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

And I can make the same operations with MS User definition - Microsoft.AspNetCore.Identity.IdentityUser (Net Core 6)

Builder.Services.AddIdentity(Of IdentityUser, IdentityRole)...

than

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

But what exactly differences? What functions I need realize by own code instead MS code?


Solution

  • I have replace default identity with MS database and MS user definition

    Builder.
    Services.
    AddDefaultIdentity(Of ApplicationUser)()
    

    to custom database (MySQL) with database first pattern (from existing user definition).

    Builder.
    Services.
    AddIdentity(Of ApplicationUser, ApplcationRole).
    AddDefaultUI().
    AddDefaultTokenProviders().
    AddUserStore(Of CustomUserStore).
    AddRoleStore(Of CustomRoleStore)()
    

    This is not a hard job. Need to realize only a couple of methods.

    Public Interface ICustomUserStore(Of T As Class)
        Inherits IUserStore(Of T)
        Inherits IUserPasswordStore(Of T)
        Inherits IUserRoleStore(Of T)
        Inherits IUserEmailStore(Of T)
        Inherits IUserPhoneNumberStore(Of T)
        Inherits IUserAuthenticatorKeyStore(Of T) 'TwoFactor
        Inherits IUserTwoFactorStore(Of T)        'TwoFactor
        Inherits IUserTwoFactorRecoveryCodeStore(Of T)        'TwoFactor
        Inherits IUserLoginStore(Of T) 'download personal data
        'IUserPasswordStore
        Overloads Function SetPasswordHashAsync(user As T, passwordHash As String, cancellationToken As CancellationToken) As Task
        Overloads Function GetPasswordHashAsync(user As T, cancellationToken As CancellationToken) As Task(Of String)
        Overloads Function HasPasswordAsync(user As T, cancellationToken As CancellationToken) As Task(Of Boolean)
        'IUserRoleStore
        Overloads Function AddToRoleAsync(user As T, roleName As String, cancellationToken As CancellationToken) As Task
        Overloads Function RemoveFromRoleAsync(user As T, roleName As String, cancellationToken As CancellationToken) As Task
        Overloads Function GetRolesAsync(user As T, cancellationToken As CancellationToken) As Task(Of IList(Of String))
        Overloads Function IsInRoleAsync(user As T, roleName As String, cancellationToken As CancellationToken) As Task(Of Boolean)
        Overloads Function GetUsersInRoleAsync(roleName As String, cancellationToken As CancellationToken) As Task(Of IList(Of T))
        'IUserEmailStore
        Overloads Function SetEmailAsync(user As T, email As String, cancellationToken As CancellationToken) As Task
        Overloads Function GetEmailAsync(user As T, cancellationToken As CancellationToken) As Task(Of String)
        Overloads Function GetEmailConfirmedAsync(user As T, cancellationToken As CancellationToken) As Task(Of Boolean)
        Overloads Function SetEmailConfirmedAsync(user As T, confirmed As Boolean, cancellationToken As CancellationToken) As Task
        Overloads Function FindByEmailAsync(normalizedEmail As String, cancellationToken As CancellationToken) As Task(Of T)
        Overloads Function GetNormalizedEmailAsync(user As T, cancellationToken As CancellationToken) As Task(Of String)
        Overloads Function SetNormalizedEmailAsync(user As T, normalizedEmail As String, cancellationToken As CancellationToken) As Task
    End Interface
    

    To form I have injected

    @inject SignInManager<ApplicationUser> SignInManager
    @inject UserManager<ApplicationUser> UserManager
    

    And my ApplicationUser is not inherits from Microsoft.AspNetCore.Identity.IdentityUser

    All working fine, even including OWIN and 2FA. Working on VB.NET, Linux platform and without MS database, without EF code First, with existing site databases.

    DefaultUI with custom story