WebAuthn
Features
Code Example
How to use
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
// Create a random challenge (base64 string is typical)
// Use a 33-byte challenge to avoid Base64 padding (length divisible by 3).
var challengeBytes = new byte[33];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(challengeBytes);
}
var challenge = Convert.ToBase64String(challengeBytes);
var rp = new RelyingParty("example.com", "Example App");
// Use a 12-byte user id to avoid Base64 padding.
var userIdBytes = Encoding.UTF8.GetBytes("user-123-abc");
var user = new PublicKeyCredentialUserEntity(
id: Convert.ToBase64String(userIdBytes),
name: "[email protected]",
displayName: "User");
var publicKeyCredentialParameters = new[]
{
new PublicKeyCredentialParameters(COSEAlgorithmIdentifier.ES256),
new PublicKeyCredentialParameters(COSEAlgorithmIdentifier.RS256),
};
var authenticatorSelection = new AuthenticatorSelectionCriteria(
AuthenticatorAttachment.Platform,
residentKey: "",
requireResidentKey: false,
userVerification: ResidentKeyRequirement.Preferred);
var timeout = 60000;
var response = await WebAuthn.CreateAsync(
challenge,
rp,
user,
publicKeyCredentialParameters,
authenticatorSelection,
timeout,
AttestationConveyancePreference.None);
// Persist response.AuthenticatorData.PublicKey and
// verify response.ClientData.Challenge on the server.