Installation
Aufy is built on top of ASP.NET Core Identity framework, which is database agnostic. Currently only Entity Framework Core implementation is available, but you can use any other database provider.
Install nuget package
dotnet add package Aufy.EntityFrameworkCore
Create or update your user class
Create a new user class that extends IdentityUser
and implements IAufyUser
interface.
public class MyUser: IdentityUser, IAufyUser{ public string MyProperty { get; set; }}
If you already have a user class that extends IdentityUser
, you can just implement IAufyUser
interface.
Create or update you DbContext
Create a new DbContext that extends one of the IdentityDbContext classes IdentityDbContext<TUser, ...>
where TUser
is your user class.
DbContext should also implement IAufyDbContext<TUser>
interface.
Next override OnModelCreating
method and apply Aufy model configuration using builder.ApplyAufyModel()
extension method.
public class MyAuthDbContext(DbContextOptions<MyAuthDbContext> options) : IdentityDbContext<MyUser>(options), IAufyDbContext<MyUser>{ public DbSet<AufyRefreshToken> RefreshTokens { get; set; }
protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder);
builder.ApplyAufyModel(); }}
If you already have a DbContext that extends IdentityDbContext
, you can just implement IAufyDbContext<TUser>
interface and apply Aufy model configuration.
Register your DbContext in DI container
Sample registration with SQLite database:
{ "ConnectionStrings": { "DefaultConnection": "Data Source=auth.db" }}
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
builder.Services.AddDbContext<MyAuthDbContext>(options => options.UseSqlite(connectionString));
Register Aufy services in DI container
builder.Services .AddAufy<AufyUser>(builder.Configuration, opts => { /* configure options here */ }) .AddEntityFrameworkStore<MyAuthDbContext, AufyUser>()
Add JWT Bearer configuration
{ "Aufy": { "JwtBearer": { "SigningKey": "super secret key", "Issuer": "MY_ISSUER", "Audience": "MY_AUDIENCE" } }}
Add authentication and authorization middleware and Aufy endpoints
app.UseAuthentication();app.UseAuthorization();
app.MapAufyEndpoints();
Add migrations and update database
dotnet ef migrations add MIGRATION_NAMEdotnet ef database update