Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

QrCode

Since 0.1.0-alpha.21
Shows text, such as a link, as a QR code to scan.

A QR code shows text, such as a link, for a phone's camera to read. It renders on the server as an SVG, so it works with JavaScript off, scales without blurring, and prints.

It's dark on light in both color schemes, since not every scanner reads light on dark. For a product or shipping number read by a barcode scanner, see Barcode.

Set Value to the text, and Label to what scanning it does, such as "QR code for this page". Screen readers hear the label, never the value. Beside the code, write what a user without a camera needs, such as the link.

Static

Preview

Open this page on your phone

izzyblazor.com/docs/components/qr-code

Code

Usage.razor
@* A link to scan, with the link written beside it for anyone without a camera. *@
<Row Gap="4" Align="Align.Center" Wrap>
    <QrCode Value="https://izzyblazor.com/docs/components/qr-code" Label="QR code for this page" Size="8.Rem" />
    <Stack Gap="1">
        <TextBlock Weight="FontWeight.Medium">Open this page on your phone</TextBlock>
        <Anchor Href="#">izzyblazor.com/docs/components/qr-code</Anchor>
    </Stack>
</Row>

Add this example to a page with the izzy CLI:

bash
izzy add component QrCode --at <file>:<line>

Setting up two-factor sign-in

An authenticator app scans an otpauth://totp/ link that contains a secret key, then shows a new six-digit code every 30 seconds. Show the key beside the QR code, in groups of four, for a user who types it in instead. Then ask for the first code in a PinField, to confirm the app has the key.

Static

Preview

Scan the code with your authenticator app

Or enter this key. Spaces and case don't matter.

jbsw y3dp ehpk 3pxp

Code

TwoFactor.razor
@using System.Buffers.Binary
@using System.ComponentModel.DataAnnotations
@using System.Security.Cryptography

<Stack Gap="4" MaxWidth="32.Rem">
    <Row Gap="4" Align="Align.Center" Wrap>
        <QrCode Value="@AuthenticatorUri" Label="QR code for your authenticator app" Size="9.Rem" />
        <Stack Gap="2">
            <TextBlock Weight="FontWeight.Medium">Scan the code with your authenticator app</TextBlock>
            <TextBlock Size="TextSize.Sm" Color="Color.Muted">Or enter this key. Spaces and case don't matter.</TextBlock>
            <Row Gap="2" Align="Align.Center">
                <Code>@GroupedKey</Code>
                <CopyButton Text="@Key" Label="Copy the key" Size="ControlSize.Sm" />
            </Row>
        </Stack>
    </Row>
    <Form Model="Model" FormName="qr-code-two-factor" OnValidSubmit="Verify">
        <PinField @bind-Value="Model!.Code" />
        <Row><Button Submit>Verify</Button></Row>
        @if (_verified is true)
        {
            <Alert Status="Status.Success">Verified. Two-factor sign-in is on.</Alert>
        }
        else if (_verified is false)
        {
            <Alert Status="Status.Danger">That code didn't work. Codes change every 30 seconds; enter the one the app shows now.</Alert>
        }
    </Form>
</Stack>

@code {
    [SupplyParameterFromForm(FormName = "qr-code-two-factor")]
    private Setup? Model { get; set; }

    private bool? _verified;

    // A real app makes a key for each user (with ASP.NET Core Identity, UserManager's
    // ResetAuthenticatorKeyAsync) and keeps it secret, and turns down a code used once already.
    // This key is the same for everyone, to try.
    private const string Key = "JBSWY3DPEHPK3PXP";

    // Google's Key Uri Format: the issuer before the account and again as a parameter.
    private static readonly string AuthenticatorUri =
        $"otpauth://totp/IzzyBlazor:{Uri.EscapeDataString("ada@example.com")}?secret={Key}&issuer=IzzyBlazor";

    private static readonly string GroupedKey = string.Join(' ', Key.Chunk(4).Select(c => new string(c))).ToLowerInvariant();

    protected override void OnInitialized() => Model ??= new();

    private void Verify() => _verified = Matches(Model!.Code!);

    public sealed class Setup
    {
        [Required, Display(Name = "Code from the app")]
        public string? Code { get; set; }
    }

}

Static: Verify posts the code without a full reload.

The link contains the secret, so don't log it or put it in a URL. The link is otpauth://totp/Issuer:account?secret=KEY&issuer=Issuer, with your site's name as the issuer in both places and the issuer and account URL-encoded.

With ASP.NET Core Identity, UserManager.ResetAuthenticatorKeyAsync makes the key, GetAuthenticatorKeyAsync reads it, and VerifyTwoFactorTokenAsync, with the authenticator token provider, checks the code. A real check also rejects a code that was already used, so a code seen over a shoulder can't be used again.

Size and error correction

To set the width and height, set Size; it's 10rem by default, and the code scales to fit. Scanners look for the light border around the code, 4 modules (squares) wide by default (QuietZone), so keep it. Longer text makes a denser code with smaller modules, so keep links short.

ErrorCorrection sets how much of the code can be damaged and still scan: Medium by default, about 15%. A higher level makes the code denser. When a higher level fits in the same size, it's used anyway.

Mode support

What works in each mode, QrCode
FeatureStaticInteractive
EverythingYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Accessibility

  • It's an image (role="img") named by Label, which is required; say what scanning it does, not what it holds.
  • The value is never read out: a link read letter by letter helps no one, and an authenticator's contains its secret. Put what a user without a camera needs, such as the link or the setup key, in text beside it.
  • Set Decorative when the text beside it already says what it is, so it's skipped.
  • It's dark on light in both color schemes, since not every scanner reads light on dark.

Parameters

QrCode parameters
PropTypeDefault
ValueThe text to encode, such as a URL.stringRequired
LabelWhat scanning it does, for screen readers, such as "QR code for your authenticator app".string?
DecorativeHides it from screen readers, for when the text beside it says what it is.boolfalse
ErrorCorrectionHow much of the code can be damaged and still scan.QrErrorCorrectionQrErrorCorrection.Medium
SizeThe width and height, quiet zone included.Length?computed
QuietZoneThe light border around the code, in modules.int4

See also

  • Image: loads an image lazily and keeps its space.

Connection lost

Rejoining the server…