Class MfaService
Orchestrates MFA operations: delegates to implementations,
manages PROPS persistence, lockout, recovery codes, and OTP challenge dispatch.
public sealed class MfaService
Inheritance
Methods
AttachWebAuthn(IWebAuthnChallengeStore, IdentityWebAuthnOptions)
Property setter for DI activation \u2014 called from RedbIdentityServiceExtensions
public void AttachWebAuthn(IWebAuthnChallengeStore challengeStore, IdentityWebAuthnOptions options)
BeginWebAuthnAssertionAsync(long, string?, string?, CancellationToken)
MFA-3: starts a WebAuthn assertion ceremony for an authenticated user (login flow or
public Task<(AssertionOptions Options, string MfaStateToken)?> BeginWebAuthnAssertionAsync(long userId, string? username, string? returnUrl, CancellationToken ct = default)
BeginWebAuthnRegistrationAsync(long, string, string?, CancellationToken)
MFA-3: starts a WebAuthn registration ceremony. Returns
public Task<(CredentialCreateOptions Options, string SetupToken)> BeginWebAuthnRegistrationAsync(long userId, string username, string? displayName, CancellationToken ct = default)
CompleteWebAuthnAssertionAsync(long, AuthenticatorAssertionRawResponse, MfaState, CancellationToken)
MFA-3: completes a WebAuthn assertion. Caller must hold a transaction with a row-lock
public Task<(bool Success, string? ErrorCode)> CompleteWebAuthnAssertionAsync(long userId, AuthenticatorAssertionRawResponse assertionResponse, MfaState state, CancellationToken ct = default)
CompleteWebAuthnRegistrationAsync(long, AuthenticatorAttestationRawResponse, string, string?, CancellationToken)
MFA-3: completes a WebAuthn registration ceremony. Verifies the attestation, runs the
public Task<(bool Success, string[]? RecoveryCodes, string? CredentialKey, string? ErrorCode)> CompleteWebAuthnRegistrationAsync(long userId, AuthenticatorAttestationRawResponse attestationResponse, string setupToken, string? credentialDisplayName, CancellationToken ct = default)
ConfirmSetupAsync(long, string, string, string, MfaState?, CancellationToken)
Confirms MFA setup by verifying the first code against the candidate secret/destination
public Task<string[]?> ConfirmSetupAsync(long userId, string methodId, string code, string setupToken, MfaState? state = null, CancellationToken ct = default)
CreateChallengeAsync(long, string, string, string?, CancellationToken)
Generates a fresh OTP code for SMS/Email, dispatches it via the matching delivery channel,
public Task<MfaChallengeResult> CreateChallengeAsync(long userId, string username, string methodId, string? returnUrl, CancellationToken ct = default)
CreateChallengeAsync(long, string, string, string?, string[]?, CancellationToken)
Overload that lets callers reuse the methods array from the original MFA state, avoiding
public Task<MfaChallengeResult> CreateChallengeAsync(long userId, string username, string methodId, string? returnUrl, string[]? knownMethods, CancellationToken ct = default)
DeleteWebAuthnCredentialAsync(long, string, CancellationToken)
MFA-3: deletes one credential. If this was the last credential and no other MFA method
public Task<bool> DeleteWebAuthnCredentialAsync(long userId, string credentialKey, CancellationToken ct = default)
DisableAsync(long, string, CancellationToken)
Disables a specific MFA method for the user.
public Task DisableAsync(long userId, string methodId, CancellationToken ct = default)
GetEnabledMethodsAsync(long, CancellationToken)
Returns the list of confirmed MFA method IDs for the user.
public Task<string[]> GetEnabledMethodsAsync(long userId, CancellationToken ct = default)
GetRegisteredMethod(string)
Returns the registered by id (public for processors).
public IMfaMethod GetRegisteredMethod(string methodId)
GetStatusAsync(long, CancellationToken)
Returns MFA status for the user: enabled, methods, recovery codes remaining.
public Task<MfaStatusResult> GetStatusAsync(long userId, CancellationToken ct = default)
IsMfaEnabledAsync(long, CancellationToken)
Whether the user has at least one confirmed MFA method.
public Task<bool> IsMfaEnabledAsync(long userId, CancellationToken ct = default)
ListWebAuthnCredentialsAsync(long, CancellationToken)
MFA-3: lists registered WebAuthn credentials for the given user.
public Task<IReadOnlyList<WebAuthnCredentialSummary>> ListWebAuthnCredentialsAsync(long userId, CancellationToken ct = default)
LoadMfaStatusAsync(long, CancellationToken)
Atomic accessor (B9 / BUG-4): returns both the enabled flag and the confirmed
public Task<(bool Enabled, string[] Methods)> LoadMfaStatusAsync(long userId, CancellationToken ct = default)
LoadPropsAsync(long, CancellationToken)
Loads MFA props for a user (public so processors can read state for verify).
public Task<MfaProps?> LoadPropsAsync(long userId, CancellationToken ct = default)
ProtectState(MfaState)
Encrypts MFA state for the challenge flow (password verified → awaiting MFA code).
public string ProtectState(MfaState state)
RegenerateRecoveryCodesAsync(long, CancellationToken)
Regenerates recovery codes. Old codes are invalidated.
public Task<string[]?> RegenerateRecoveryCodesAsync(long userId, CancellationToken ct = default)
RenameWebAuthnCredentialAsync(long, string, string?, CancellationToken)
MFA-3: renames a single credential (the friendly display label).
public Task<bool> RenameWebAuthnCredentialAsync(long userId, string credentialKey, string? newDisplayName, CancellationToken ct = default)
SetupAsync(long, string, string, string?, CancellationToken)
Initiates MFA setup for a method. Returns setup data (QR URI, masked destination, etc.)
public Task<MfaSetupResult> SetupAsync(long userId, string methodId, string username, string? destination = null, CancellationToken ct = default)
UnprotectState(string)
Decrypts and validates MFA state. Returns null if invalid/expired.
public MfaState? UnprotectState(string protectedState)
VerifyAsync(long, string, string, MfaState?, CancellationToken)
Verifies an MFA code during login. Enforces lockout after too many failures.
public Task<bool> VerifyAsync(long userId, string methodId, string code, MfaState? state = null, CancellationToken ct = default)
VerifyRecoveryCodeAsync(long, string, CancellationToken)
Verifies a one-time recovery code. The code is consumed (removed) on success.
public Task<bool> VerifyRecoveryCodeAsync(long userId, string code, CancellationToken ct = default)