{"record":{"id":"34e80ac655ef1dda","repo":"TechnitiumSoftware/DnsServer","slug":"username-can-contain-only-alpha-numeric","errorCode":null,"errorMessage":"Username can contain only alpha numeric, '@', '-', '_', or '.' characters.","messagePattern":"Username can contain only alpha numeric, '@', '-', '_', or '\\.' characters\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Auth/User.cs","lineNumber":208,"sourceCode":"                    continue;\n\n                if ((c >= 48) && (c <= 57)) //[0-9]\n                    continue;\n\n                if (c == '@')\n                    continue;\n\n                if (c == '-')\n                    continue;\n\n                if (c == '_')\n                    continue;\n\n                if (c == '.')\n                    continue;\n\n                if (throwException)\n                    throw new ArgumentException(\"Username can contain only alpha numeric, '@', '-', '_', or '.' characters.\", nameof(Username));\n\n                return false;\n            }\n\n            return true;\n        }\n\n        #endregion\n\n        #region internal\n\n        internal void SetUsername(string username)\n        {\n            if (_passwordHashType == UserPasswordHashType.OldScheme)\n                throw new InvalidOperationException(\"Cannot change username when using old password hash scheme. Change password once and try again.\");\n\n            IsUsernameValid(username, true);\n","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Auth/User.cs#L190-L226","documentation":"Thrown by User.IsUsernameValid(username, throwException: true) when the username contains any character outside the allowed set: a-z, A-Z, 0-9, '@', '-', '_', '.'. The validator iterates each character and rejects the first disallowed one. It is an ArgumentException (parameter Username).","triggerScenarios":"Calling IsUsernameValid(username, true) with a username containing spaces, unicode, or punctuation other than '@','-','_','.' (e.g. parentheses, slashes, colons).","commonSituations":"Usernames derived from email addresses that retain disallowed punctuation; non-ASCII/unicode display names passed as usernames; spaces or tabs accidentally included via copy-paste; UPNs with characters the IdP allows but this server does not.","solutions":["Restrict usernames to [a-zA-Z0-9@-_.] only; strip or reject other characters.","Normalize the input (e.g. take the local part of an email) before validating.","Surface the allowed-character set to the user in the UI so they self-correct."],"exampleFix":"// before\nUser.IsUsernameValid(username, throwException: true);\n\n// after\nstatic bool AllowedUsername(string s) => !string.IsNullOrEmpty(s)\n    && s.All(c => char.IsLetterOrDigit(c) || \"@-_./\".IndexOf(c) >= 0);\nif (!AllowedUsername(username))\n    return BadRequest(\"Username can contain only alpha numeric, '@', '-', '_', or '.' characters.\");","handlingStrategy":"type-guard","validationCode":"static bool IsValidUsernameChars(string s) =>\n    !string.IsNullOrEmpty(s) && s.All(c =>\n        (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||\n        (c >= '0' && c <= '9') || c == '@' || c == '-' || c == '_' || c == '.');\n\nif (!IsValidUsernameChars(username))\n    return BadRequest(\"Username can contain only alpha numeric, '@', '-', '_', or '.' characters.\");","typeGuard":"static bool IsValidUsername(string s) =>\n    !string.IsNullOrWhiteSpace(s)\n    && s.Length <= 255\n    && s.All(c => char.IsLetterOrDigit(c) || \"@-_./\".IndexOf(c) >= 0);","tryCatchPattern":"try { User.IsUsernameValid(username, throwException: true); }\ncatch (ArgumentException ex) when (ex.ParamName == \"Username\" && ex.Message.Contains(\"alpha numeric\"))\n{ return BadRequest(ex.Message); }","preventionTips":["Document the allowed character set to end users.","Sanitize imported usernames (strip spaces/unicode/disallowed punctuation).","Use the non-throwing IsUsernameValid overload to validate before assignment."],"tags":["auth","user-management","validation","character-encoding"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}