An Entity Framework Core store for Fido2 (WebAuthn / passkeys).
Provide a better integration between the Fido2 library and ASP.NET Core Identity.
| NetDevPack.Fido2.EntityFramework.Store | Fido2 | Target framework |
|---|---|---|
| 4.x | 4.x | .NET 10 (LTS) |
| 3.x | 3.x | .NET 8, .NET 9, .NET 10 |
- Add to
Program.cs
builder.Services.AddFido2(builder.Configuration.GetSection("Fido2"));
builder.Services.AddFido2Context<ApplicationDbContext>();- Change
ApplicationDbContext
using NetDevPack.Fido2.EntityFramework.Store.Model;
using NetDevPack.Fido2.EntityFramework.Store.Store;
public class ApplicationDbContext : IdentityDbContext, IFido2Context
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<StoredCredentialDetail> Fido2StoredCredential { get; set; }
}- Inject
IFido2Storeto save and recover credentials:
// Registration
var credential = await _fido2.MakeNewCredentialAsync(new MakeNewCredentialParams
{
AttestationResponse = attestationResponse,
OriginalOptions = options,
IsCredentialIdUniqueToUserCallback = async (args, ct) =>
!(await _fido2Store.ListCredentialsByPublicKeyIdAsync(args.CredentialId)).Any()
});
_fido2Store.Store("My security key", fidoUser, StoredCredential.FromRegisteredCredential(credential));
// Login
var stored = await _fido2Store.GetCredentialByPublicKeyIdAsync(assertionResponse.RawId);
var result = await _fido2.MakeAssertionAsync(new MakeAssertionParams
{
AssertionResponse = assertionResponse,
OriginalOptions = options,
StoredPublicKey = stored.PublicKey,
StoredSignatureCounter = stored.SignatureCounter,
IsUserHandleOwnerOfCredentialIdCallback = async (args, ct) =>
(await _fido2Store.GetCredentialsByUserHandleAsync(args.UserHandle)).Any(c => c.Descriptor.Id.SequenceEqual(args.CredentialId))
});
await _fido2Store.UpdateCounter(result.CredentialId, result.SignCount, result.IsBackedUp);4.x targets .NET 10 and Fido2 4. Stay on 3.x for .NET 8/9 or Fido2 3.
Fido2NetLib.Development.StoredCredentialdoes not exist in Fido2 4. The store now uses its ownNetDevPack.Fido2.EntityFramework.Store.Model.StoredCredential. It keeps the 3.x property names (UserId,Descriptor,PublicKey,UserHandle,SignatureCounter,CredType,RegDate,AaGuid). Replaceusing Fido2NetLib.Development;withusing NetDevPack.Fido2.EntityFramework.Store.Model;.- New
StoredCredential.FromRegisteredCredential(RegisteredPublicKeyCredential)builds a credential from the result ofIFido2.MakeNewCredentialAsync, including transports and backup flags. IFido2Store.GetCredentialByPublicKeyIdAsyncreturnsStoredCredential?.IFido2Store.UpdateCounter(byte[] credentialId, uint counter, bool? isBackedUp = null)also stores the Backup State flag.- Fido2 4 itself changed its API:
MakeNewCredentialAsync,MakeAssertionAsync,RequestNewCredentialandGetAssertionOptionsnow take*Paramsobjects,MakeAssertionAsyncthrowsFido2VerificationExceptioninstead of returning a status, and options no longer carryStatus/ErrorMessage. UseAuthenticatorAssertionRawResponse.RawId(bytes) to look up credentials, sinceIdis now a string. See the Fido2 releases.
Two columns were added to StoredCredentialDetail. Nothing was renamed or removed, and existing rows keep working:
| Column | Type | Default for existing rows |
|---|---|---|
IsBackupEligible |
bool (not null) |
false |
IsBackedUp |
bool (not null) |
false |
Create a migration in your application:
dotnet ef migrations add Fido2StoreV4 -c ApplicationDbContext
dotnet ef database update -c ApplicationDbContextThe generated migration only adds the two columns with defaultValue: false. For SQL Server it is equivalent to:
ALTER TABLE [Fido2StoredCredential] ADD [IsBackupEligible] bit NOT NULL DEFAULT CAST(0 AS bit);
ALTER TABLE [Fido2StoredCredential] ADD [IsBackedUp] bit NOT NULL DEFAULT CAST(0 AS bit);IsBackedUp is refreshed on the next successful login when you pass result.IsBackedUp to UpdateCounter. IsBackupEligible cannot be known for credentials registered with 3.x, so it stays false for them.
See the demo in demo/Fido2.Passwordless. Docker build context is the repository root:
docker build -f demo/Fido2.Passwordless/Dockerfile .