A default implementation for authentication using jwt and refresh tokens with identity core. It also allows for easy extending with your own user properties and dbsets.
Using dependency injection:
AddDefaultJwtAuthentication<TUserAuthenticationService>(this IServiceCollection services,
TokenValidationParameters parameters) where TUserAuthenticationService : class, IUserAuthenticationService
AddDefaultJwtDbContext<TAppUser, TDbContext>(this IServiceCollection services,
string connectionString) where TAppUser : AppUser where TDbContext : AbstractAppDbContext<TAppUser>
For this to work IUserAuthenticationService
, AppUser
, AbstractAppDbContext<TAppUser>
must be implemented.
This is the service which handles the authentication of users. It is recommended to use UserRepository<T>
to interact with the db.
Authenticate
In this method the AppUserLogin
attempt should be authenticated. Usage of IUserRepository<T>.Authenticate
is recommended.
Create
In this method the AppUserCreate
attempt should be used to create a user. Usage of IUserRepository<T>.Create
is recommended.
Refresh
In this method the jwt and the refresh token should be used to authenticate a user. Usage of IUserRepository<T>.RefreshToken
is recommended.
An abstract class which extends the IdentityUser
class from Microsoft.AspNetCore.Identity
with the following fields FirstName
, LastName
and RefreshTokens
.
The AppUser
class should be extended with a custom user class. If needed, additional properties can be added to the class.
An abstract class which extends the IdentityDbContext<T>
class from Microsoft.AspNetCore.Identity
with a DbSet of refresh tokens.
The AbstractAppDbContext<TAppUser>
class should be extended with a DbContext class. If needed, additional DbSets can be added. Of course, TAppUser should be your own implementation of AppUser
as mentioned before.
A data object used to return the results of actions performed by the IUserRepository
.
AppUserCreate
and AppUserLogin
can both be extended with custom properties.
Written for version 1.0.0