{"record":{"id":"14fa7f30f187937e","repo":"dotnet/aspnetcore","slug":"please-enter-a-username","errorCode":null,"errorMessage":"Please enter a username.","messagePattern":"Please enter a username\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/Identity/samples/IdentitySample.PasskeyUI/wwwroot/app.js","lineNumber":32,"sourceCode":"    function enableRouteScripts() {\n        Blazor.addEventListener('enhancednavigationend', executeScript);\n\n        if (document.readyState === 'loading') {\n            document.addEventListener('DOMContentLoaded', executeScript);\n        } else {\n            executeScript();\n        }\n    }\n\n    // Define home page JS functionality.\n    addRouteScript('/', async () => {\n        let abortController;\n        const form = document.getElementById('auth-form');\n        const statusMessage = document.getElementById('status-message');\n\n        async function fetchNewCredential(username) {\n            if (!username) {\n                throw new Error('Please enter a username.');\n            }\n\n            const optionsResponse = await fetch('/attestation/options', {\n                method: 'POST',\n                body: JSON.stringify({\n                    username,\n                }),\n                headers: {\n                    'Content-Type': 'application/json',\n                },\n                credentials: 'include',\n            });\n            const optionsJson = await optionsResponse.json();\n            const options = PublicKeyCredential.parseCreationOptionsFromJSON(optionsJson);\n            abortController?.abort();\n            abortController = new AbortController();\n            return await navigator.credentials.create({\n                publicKey: options,","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Identity/samples/IdentitySample.PasskeyUI/wwwroot/app.js#L14-L50","documentation":"Thrown by fetchNewCredential() in the sample PasskeyUI app.js (line 32) when the username passed to register a new WebAuthn/passkey credential is empty. The /attestation/options endpoint requires a username to build the credential creation options (it becomes the credential's user.id/user.name), so the client guards early and throws before making the request.","triggerScenarios":"The register submit button is clicked while the username input is empty; FormData(form).get('username') returns '' or null and fetchNewCredential throws 'Please enter a username.'.","commonSituations":"User clicks 'Register' without typing a username. Input's name attribute is not 'username' so FormData returns null even when text was entered. Validation feedback not shown before the WebAuthn ceremony. Copy of the sample script adapted without preserving the username field name.","solutions":["Ensure the username input has name=\"username\" and is non-empty before submitting.","Add client-side UI validation (disable the register button until a username is present, or validate on submit and show a message).","If you renamed the field, update the FormData lookup or the guard to match the new name.","Display the error in the status-message element so the user understands the input is required."],"exampleFix":"// before\n<input name=\"user\" /> <!-- wrong name, FormData.get('username') → null -->\n\n// after\n<input name=\"username\" required />\n<button name=\"action\" value=\"register\" disabled id=\"btn-register\">Register</button>\n<script>document.getElementById('username').addEventListener('input', e => document.getElementById('btn-register').disabled = !e.target.value);</script>","handlingStrategy":"validation","validationCode":"function readUsername(form: HTMLFormElement): string | null {\n  const v = new FormData(form).get('username');\n  return typeof v === 'string' && v.trim().length > 0 ? v : null;\n}","typeGuard":"function isNonEmptyUsername(v: FormDataEntryValue | null): v is string {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":null,"preventionTips":["Give the username input name=\"username\" and required.","Disable the Register button until a username is entered.","Show inline validation before invoking WebAuthn.","Display thrown errors in the status-message element."],"tags":["webauthn","passkey","identity","forms","sample","validation"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}