Passkey
Namespace: Wisej.Web
Assembly: Wisej.Framework (4.2.0.0)
Registers passkeys (WebAuthn Level 3 discoverable credentials) for an account and signs users in with them, verifying every browser answer on the server and publishing the result as a PasskeyIdentity on Identity and, only when SetApplicationUser allows it, on User.
4.2.0+- C#
- VB.NET
public class Passkey : Component
Public Class Passkey
Inherits Component
Register an IPasskeyCredentialStore with Services, drop the component on a page, and set AllowedOrigins to the origins the application is served at. Left empty, together with RelyingPartyId, the accepted origin is whichever one the session's client claims and every port of that host shares the passkeys, which serves development but not production. Call RegisterAsync for a user the application has already signed in and authorized — from an account-settings page, never from an anonymous sign-up screen — and AuthenticateAsync from the sign-in page. GetCredentialsAsync, RenameCredentialAsync and DeleteCredentialAsync act on whatever account they are given: pass the signed-in account, never an id the browser supplies. Await the tasks or wire Authenticated and AuthenticationFailed; both are raised, in that order, before the task settles, unless the session has ended by then (see AuthenticationFailed). Sign the user out with SignOut on the component that signed them in, and keep that component alive for as long as the user may sign out: disposing it does not sign the user out, and no other instance can.
Verifies ES256 and RS256 credentials with attestation "none", one ceremony at a time per component, entirely within one awaited server call: no challenge is stored anywhere, and a ceremony that outlives its server process fails before anything is persisted. Set RelyingPartyId explicitly behind a proxy, on a host with aliases, or when several hosts share one store; AllowedOrigins is required in production. Not a FIDO-certified server: direct attestation, metadata, EdDSA, conditional mediation and related origins are not offered.
Constructors
Passkey()
Initializes a new instance of the Passkey class.
Passkey(container)
Initializes a new instance of the Passkey class together with the specified container.
| Name | Type | Description |
|---|---|---|
| container | IContainer | An IContainer that represents the container for the component. |
Properties
AllowedOrigins
String[]: Returns or sets the exact origins that may complete a passkey ceremony. Empty means the current origin only. (Default: null)
Read at the start of every operation. Null or empty means the scheme, host and port the browser opened the application at, as the framework computed them from the request. That default is what the session's client claims, not something the server checks: a client can name another origin of the same host, such as another port, and an assertion relayed from that origin is then accepted. Set this property in production, and whenever RelyingPartyId is set: an empty list then fails the operation with Configuration. Each entry is an absolute origin — https://accounts.example.com — with no path, query, fragment or user information, https unless the host is localhost; it is normalized (host lower-cased, default port dropped) and then compared with the browser's client data origin exactly. A host that carries U+00DF, U+1E9E, U+03C2, U+200C or U+200D is refused on every target and is written in its xn-- form. A malformed entry fails the operation with Configuration naming this property. Every entry must be compatible with the effective RelyingPartyId: its host equal to it or ending in it. This is an exact list, not a related-origin request, which v1 does not offer.
AuthenticatorAttachment
PasskeyAuthenticatorAttachment: Returns or sets whether a new passkey may be created only on the device itself or only on a roaming authenticator. (Default: Any)
Registration only. Any sends no authenticatorAttachment member, so platform authenticators, security keys and cross-device flows all take part. An authentication ceremony accepts whatever authenticator holds the credential.
Identity
PasskeyIdentity: Returns the identity from the last successful passkey sign-in on this component, or null.
Null before any sign-in and after SignOut. Registration does not set it.
IsAuthenticated
Boolean: Returns whether this component holds an authenticated identity.
RelyingPartyId
String: Returns or sets the relying party identifier: the domain the passkeys are scoped to. Empty means the host of the current origin. (Default: null)
Read at the start of every operation, trimmed and lower-cased: null, empty or white space means the host of the origin the session's client claimed; a value must be a valid domain equal to the host of every allowed origin or a registrable suffix of it, else the operation fails with Configuration naming this property. A value also requires AllowedOrigins: with an empty list the operation fails the same way, because that list's default is the origin the session's client claims and a value accepts every host under it. A name that carries U+00DF, U+1E9E, U+03C2, U+200C or U+200D is refused on every target, because the targets map those characters differently: write it in its xn-- form. Every credential, user handle and store call is scoped by the effective value, so changing it after passkeys exist strands them. Set it explicitly behind a proxy that rewrites hosts, on a host reachable under aliases, or when one store serves several hosts.
RelyingPartyName
String: Returns or sets the relying party name the browser shows while a passkey is created. Empty means the relying party identifier. (Default: null)
ResidentKey
PasskeyResidentKey: Returns or sets whether a new passkey must be discoverable, so the user can sign in without typing an account name. (Default: Required)
Registration only. Sent as the residentKey requirement, with requireResidentKey true if and only if the value is Required. A credential created under Discouraged can be used only by AuthenticateAsync.
Tag
Object: Returns or sets application-defined data associated with the component. (Default: null)
Timeout
Int32: Returns or sets how long the browser may take to answer a passkey request, in milliseconds. (Default: 300000)
The component's own timer fires when this has elapsed. The browser receives this value plus a fixed 5000 ms grace, so the component owns the typed Timeout result and any later browser answer is ignored. The default, five minutes, is the WebAuthn Level 3 recommendation. Keep this value below the application's idle session timeout: a page showing an operating system passkey prompt sees no user activity, and a session that expires first ends the application before the component can report anything.
UserVerification
PasskeyUserVerification: Returns or sets whether the authenticator must verify the user with a PIN, fingerprint, face or device unlock. (Default: Required)
Sent as the userVerification requirement of both ceremonies and enforced on the answer: with Required an assertion or attestation whose UV flag is clear fails with UserVerificationRequired, and so does a sign-in with a credential registered without user verification. With either other value the ceremony succeeds without verification and UserVerified says what happened. Changing it to Required later locks out the passkeys registered without verification until their users register new ones. Before that, the application removes each old one with DeleteCredentialAsync; GetCredentialsAsync lists it with UserVerified false. Under Required a registration leaves such a passkey out of the exclude list, so the authenticator that holds it can create the new one.
Methods
AuthenticateAsync(cancellationToken)
Signs a user in with a discoverable passkey, without knowing the account first: the browser lets the user pick a passkey for this relying party, the store resolves the account from the credential id, and the assertion is verified on the server.
| Parameter | Type | Description |
|---|---|---|
| cancellationToken | CancellationToken | Cancels the ceremony: the prompt is aborted and the task canceled. |
Returns: Task<PasskeyAuthenticationResult>. A task that completes after Authenticated with the outcome, faults with a PasskeyException after AuthenticationFailed, or is canceled when the user dismissed the prompt, cancellationToken was canceled, or Timeout elapsed. When the session has ended by then, the task settles and no event is raised, except AuthenticationFailed for a failure that names its credential; see its remarks.
Sends no allowCredentials, so only a discoverable credential can answer; a credential registered with Discouraged is invisible here. The credential id the authenticator returns is looked up with FindCredentialAsync; the user handle it returns must equal the stored one exactly. Then, in this order: the assertion is verified against the stored key and the component's policy, Authenticating is raised and may veto, the new counter and flags are committed with CommitAuthenticationAsync, the principal is assigned to User unless SetApplicationUser is false — its pre-fill when an authenticated principal this component did not set holds it — or such a principal appeared during the commit, the session id is refreshed, and Authenticated is raised; IsApplicationUser reports which. A conflict or a vanished row at the commit fails the ceremony; nothing is assigned before the commit succeeds.
One ceremony at a time per component: calling this cancels a ceremony still in progress. One awaiting the browser fails with Cancelled at once; one awaiting the store fails with it when that call returns, unless the store reports a failure of its own, a sign-in has already failed on what the store returned — a credential it does not find, one of another account, or a record the browser's answer does not verify against, a regressed counter included — which is reported as that failure, or a registration's insert succeeded, which completes it. Always await the task; never block on it.
The pre-fill of SetApplicationUser reads User on the calling thread before the first await, where a Windows or cookie principal is visible only while a browser request runs on that thread or on a WebSocket connection. On a connection without WebSocket, a call made after an await that finished outside the request, or from an Authenticated, CredentialRegistered or AuthenticationFailed handler, cannot see the host principal, and the pre-fill is true. Under host authentication, set SetApplicationUser explicitly in Authenticating — false to keep the host principal.
Throws:
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
AuthenticateAsync(userId, cancellationToken)
Signs a known user in with one of that account's passkeys: the browser is told which credentials are acceptable, and the assertion is verified on the server against the one it used.
| Parameter | Type | Description |
|---|---|---|
| userId | String | The application user id of the account, as the application identified it — one an earlier sign-in step confirmed, or a remembered account the server issued and can verify; never a name a visitor typed or a value the browser holds unverified. |
| cancellationToken | CancellationToken | Cancels the ceremony: the prompt is aborted and the task canceled. |
Returns: Task<PasskeyAuthenticationResult>. As for AuthenticateAsync.
Sends the account's credential ids and transports as allowCredentials, so server-side and discoverable credentials alike can answer. An account with no stored credentials fails with UnknownCredential before any prompt is shown: this overload never falls back to usernameless sign-in, and a null account name is an argument error, not a mode switch. The credential the authenticator used must belong to userId , else CredentialMismatch. Everything after that is as for AuthenticateAsync.
Whoever supplies userId learns whether that account has a passkey: an account with one gets a browser prompt, an account without one fails at once with no prompt, and the account's credential ids and transports are sent to the browser as allowCredentials (WebAuthn Level 3 §14.6.2 and §14.6.3). Showing the same message for both outcomes does not hide this. On a page an unauthenticated visitor can reach, sign in with AuthenticateAsync, or name the account only after another step has identified the user.
The pre-fill of SetApplicationUser reads User on the calling thread before the first await, where a Windows or cookie principal is visible only while a browser request runs on that thread or on a WebSocket connection. On a connection without WebSocket, a call made after an await that finished outside the request, or from an Authenticated, CredentialRegistered or AuthenticationFailed handler, cannot see the host principal, and the pre-fill is true. Under host authentication, set SetApplicationUser explicitly in Authenticating — false to keep the host principal.
Throws:
- ArgumentException userId is null, empty or white space, begins or ends with white space, or is longer than 256 characters.
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
CallClientAsync(function, options)
Calls a function of the client widget and returns its answer. The base implementation is CallAsync with options as the single argument. Override in a test to answer without a browser.
| Parameter | Type | Description |
|---|---|---|
| function | String | The widget function: "create", "get" or "capabilities". |
| options | Object | The one argument the function takes: a map whose members are the wire contract's. Always one map and never several arguments, because the client core rewrites a top-level string argument that begins with "id_" into a component reference, and a base64url value can begin with those three characters. |
Returns: Task<Object>. A task that completes with the widget's envelope — a map with an "ok" member — as the framework parses it, or never completes when the browser never answers. The caller races it against Timeout and the cancellation token; this method does not.
DeleteCredentialAsync(userId, credentialId, cancellationToken)
Removes one of an account's passkeys from the store, so it can no longer sign the user in.
| Parameter | Type | Description |
|---|---|---|
| userId | String | The application user id that owns the credential, of an account the caller may manage: the signed-in user's own id as the application established it at sign-in (after a passkey sign-in, UserId), or an account the signed-in user is authorized to administer. Never a value the browser supplies — a query string, a text box, a hidden field, a cookie. |
| credentialId | Byte[] | The credential id. |
| cancellationToken | CancellationToken | Cancels the store call. |
Returns: Task<Boolean>. A task that completes with true when the credential existed under the account and was removed, false when it does not exist or belongs to another account, or faults with a PasskeyException on a configuration or store failure. The component makes no ownership check of its own. Another account's credential gives false only when the store compares user ids ordinally, as the Scope remark of IPasskeyCredentialStore requires.
A pass-through to DeleteCredentialAsync. Raises no event and does not sign anyone out. The passkey stays on the user's device or in their password manager: the application tells the user to delete it there too. The application decides whether the account keeps another way to sign in; the component cannot see passwords, OAuth sign-ins or recovery codes and never refuses to remove the last passkey. The caller has authorized the account and, for a security-sensitive setting, has re-authenticated the user recently; the component cannot.
Throws:
- ArgumentException userId is null, empty or white space, begins or ends with white space, or is longer than 256 characters, or credentialId is null, empty or longer than 1023 bytes.
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
Dispose(disposing)
Releases the resources used by this component.
| Parameter | Type | Description |
|---|---|---|
| disposing | Boolean | true when called by the application rather than a finalizer. |
Leaves User as it is: disposing the component that signed the user in does not sign the user out, and no Passkey can sign that principal out afterwards (see SignOut).
GetCapabilitiesAsync(cancellationToken)
Asks the browser what it can do, to decide whether and how to offer passkeys in the user interface.
| Parameter | Type | Description |
|---|---|---|
| cancellationToken | CancellationToken | Cancels the browser call. |
Returns: Task<PasskeyCapabilities>. A task that completes with the browser's answer, or faults with a PasskeyException carrying InvalidResponse when the widget answered nothing usable.
Shows no prompt and touches no store. Hints only: a ceremony is verified on the server whatever the browser claimed here. Bounded by a fixed ten seconds, not by Timeout: nothing here waits for the user.
Throws:
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
GetCredentialsAsync(userId, cancellationToken)
Returns the passkeys registered for an account, for an account-settings page.
| Parameter | Type | Description |
|---|---|---|
| userId | String | The application user id of an account the caller may manage: the signed-in user's own id as the application established it at sign-in (after a passkey sign-in, UserId), or an account the signed-in user is authorized to administer. Never a value the browser supplies — a query string, a text box, a hidden field, a cookie. |
| cancellationToken | CancellationToken | Cancels the store call. |
Returns: Task<PasskeyCredential[]>. A task that completes with the account's credentials under the effective RelyingPartyId, an empty array when it has none, or faults with a PasskeyException carrying Configuration, StoreUnavailable or StoreFailed.
A pass-through to GetCredentialsAsync with the relying party id resolved the way the ceremonies resolve it. Every returned record and nested array is a defensive copy. Raises no event. The caller has authorized the account; the component cannot.
Throws:
- ArgumentException userId is null, empty or white space, begins or ends with white space, or is longer than 256 characters.
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
OnAuthenticated(args)
Fires the Authenticated event.
| Parameter | Type | Description |
|---|---|---|
| args | PasskeyAuthenticatedEventArgs |
OnAuthenticating(args)
Fires the Authenticating event.
| Parameter | Type | Description |
|---|---|---|
| args | PasskeyAuthenticatingEventArgs |
OnAuthenticationFailed(args)
Fires the AuthenticationFailed event.
| Parameter | Type | Description |
|---|---|---|
| args | PasskeyAuthenticationFailedEventArgs |
OnCredentialRegistered(args)
Fires the CredentialRegistered event.
| Parameter | Type | Description |
|---|---|---|
| args | PasskeyCredentialRegisteredEventArgs |
OnWebRender(config)
Provides the client widget's configuration.
| Parameter | Type | Description |
|---|---|---|
| config | Object | The configuration. |
RegisterAsync(user, credentialName, cancellationToken)
Registers a new passkey for the specified account: builds the creation options, awaits the browser, verifies the attestation object on the server, and stores the credential.
| Parameter | Type | Description |
|---|---|---|
| user | PasskeyUser | The account the passkey is for, which the application has already authenticated and authorized. The component cannot check that; call this from a signed-in account-settings page, never from an anonymous screen. The authorization must hold until the task settles: SignOut on this component cancels the registration unless its insert is already at the store, but when the account's sign-in ends any other way, cancel cancellationToken or dispose the component, or the prompt still open can bind a passkey to this account. |
| credentialName | String | The user-visible name of the passkey — "MacBook Pro", "YubiKey 5" — at most 256 characters, or null to store none; the user can rename it later. |
| cancellationToken | CancellationToken | Cancels the ceremony: the prompt is aborted and the task canceled. Once the store has inserted the credential, canceling no longer stops the registration; see the remarks. |
Returns: Task<PasskeyRegistrationResult>. A task that completes with the stored credential after CredentialRegistered, faults with a PasskeyException after AuthenticationFailed, or is canceled when the user dismissed the prompt, cancellationToken was canceled, SignOut was called on this component, or Timeout elapsed before the credential was stored. When the session has ended by then, the task settles and no event is raised.
Fails before any prompt is shown when no store is registered or the relying party or origins are misconfigured. Obtains the account's user handle from the store (creating a 64-byte random one when the account has none), lists its existing credentials so the browser excludes them, then requests a discoverable, user-verified ES256 or RS256 credential with attestation "none" and the credProps extension, per ResidentKey, UserVerification and AuthenticatorAttachment. The stored record is the unique insert of AddCredentialAsync: a duplicate — a race, or an authenticator that ignored the exclusion — fails with CredentialExists and never touches the existing row.
One ceremony at a time per component: calling this cancels a ceremony still in progress. One awaiting the browser fails with Cancelled at once; one awaiting the store fails with it when that call returns, unless the store reports a failure of its own, a sign-in has already failed on what the store returned — a credential it does not find, one of another account, or a record the browser's answer does not verify against, a regressed counter included — which is reported as that failure, or a registration's insert succeeded, which completes it. Registration signs nobody in: Identity and User are untouched. Always await the task; never block on it.
Cancellation is not a rollback. The authenticator creates the passkey before the component stores it, and once AddCredentialAsync has inserted it the registration completes and raises CredentialRegistered, even when cancellationToken was canceled, a newer ceremony replaced this one, SignOut was called on this component or the component was disposed while the insert was in flight: the passkey exists on the user's device and in the store, and it can sign the user in. When the session itself has ended by then, the task still completes with the stored credential, but CredentialRegistered is not raised.
Throws:
- ArgumentNullException user is null.
- ArgumentException credentialName is longer than 256 characters.
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
RenameCredentialAsync(userId, credentialId, name, cancellationToken)
Renames one of an account's passkeys.
| Parameter | Type | Description |
|---|---|---|
| userId | String | The application user id that owns the credential, of an account the caller may manage: the signed-in user's own id as the application established it at sign-in (after a passkey sign-in, UserId), or an account the signed-in user is authorized to administer. Never a value the browser supplies — a query string, a text box, a hidden field, a cookie. |
| credentialId | Byte[] | The credential id. |
| name | String | The new name, at most 256 characters; null or white space clears it. |
| cancellationToken | CancellationToken | Cancels the store call. |
Returns: Task<Boolean>. A task that completes with true when the credential existed under the account and was renamed, false when it does not exist or belongs to another account, or faults with a PasskeyException on a configuration or store failure. The component makes no ownership check of its own. Another account's credential gives false only when the store compares user ids ordinally, as the Scope remark of IPasskeyCredentialStore requires.
A pass-through to RenameCredentialAsync. Raises no event. The caller has authorized the account and, for a security-sensitive setting, has re-authenticated the user recently; the component cannot.
Throws:
- ArgumentException userId is null, empty or white space, begins or ends with white space, or is longer than 256 characters; credentialId is null, empty or longer than 1023 bytes; name is longer than 256 characters.
- InvalidOperationException The component is in design mode, or the calling thread has no Wisej session.
SignOut()
Signs the user out locally: clears Identity and, when User is still the exact principal this component assigned, replaces it with an unauthenticated principal so that IsAuthenticated becomes false.
A principal assigned by anything else — host authentication, an OAuthClient, an Authenticating handler that set SetApplicationUser to false — is never touched. Refreshes the session id when the component had a signed-in user. There is no remote session to end and no event: a passkey is a key, not a session at a provider. Safe to call when not signed in; nothing happens then.
Acts only on the principal this instance published, and only until the instance is disposed: SignOut on another Passkey, or on this one after it was disposed, does nothing. Keep the component that signed the user in alive for as long as the user may sign out, for example on the main page or a long-lived container; otherwise sign out by assigning an unauthenticated principal, such as Application.User = new ClaimsPrincipal(new ClaimsIdentity()). Never assign null, which under Windows or cookie authentication falls back to the host's principal.
A ceremony in flight on this component, an authentication or a registration, is cancelled and ends as Cancelled: a registration stores nothing unless its insert had already begun when this ran, and one whose insert succeeds completes, as RegisterAsync describes. An authentication awaiting the store ends as its own failure instead when what the store returns fails it first: a credential not found, one of another account, or a record the browser's answer does not verify against. A sign-out by any other means does not reach a ceremony in flight: a registration's prompt stays open until the user answers it or Timeout elapses, and whoever answers binds a passkey to that registration's account, so cancel the token passed to RegisterAsync, or dispose the component, when the account's sign-in ends. An authentication that has already published its sign-in is not cancelled: when this runs on another thread of the session, that sign-in's Authenticated handlers may still run, and its task still complete, after this returned, reporting a sign-in this call has already cleared.
Throws:
- InvalidOperationException The calling thread has no Wisej session, or holds another session than the one the user signed in to on this component. Nothing is cleared: the user stays signed in until this is called on a thread of that session. A disposed component throws nothing.
Events
Authenticated
PasskeyAuthenticatedEventHandler Fired when the user is signed in with a passkey and the credential state is saved. Switch views here.
This event is raised outside any browser request. An application that runs without WebSocket must set pollingInterval in its entry page configuration, or the user interface changes made here reach the browser only at the user's next interaction. Nor is it serialized against the session's requests: a SignOut that ran after the sign-in was published may already have cleared it when a handler runs. Read IsAuthenticated, not IsApplicationUser, before recording application state.
Authenticating
PasskeyAuthenticatingEventHandler Fired after the assertion is verified and before anything is committed or assigned; SetApplicationUser decides whether the principal may become Application.User. Load claims, replace the principal, or veto.
AuthenticationFailed
PasskeyAuthenticationFailedEventHandler Fired when a passkey registration or sign-in ends unsuccessfully, for any reason.
This event is raised outside any browser request. An application that runs without WebSocket must set pollingInterval in its entry page configuration, or the user interface changes made here reach the browser only at the user's next interaction.
It is raised in the session the ceremony started in, before the task settles. When that session has ended by then, the task settles and no event is raised, except for a failure that names its credential — CounterRegression, StoreConflict or Rejected: this event is still raised for it, before the task settles and with no session current, so that the application can act on Credential; the handler cannot reach the ended session's user interface or User. Nothing is raised, whatever the reason, when the thread that ends the ceremony holds another live session.
CredentialRegistered
PasskeyCredentialRegisteredEventHandler Fired when a new passkey has been verified and saved for an account.
This event is raised outside any browser request. An application that runs without WebSocket must set pollingInterval in its entry page configuration, or the user interface changes made here reach the browser only at the user's next interaction.
Implements
| Name | Description |
|---|---|
| IUserData | Provides access to the UserData and Tag properties associated to the component implementing this interface. |
| IWisejComponent | All wisej components implement this interface. |
| IWisejSerializable | Allows an object to serialize itself. |