TechnitiumSoftware/DnsServer · error · ArgumentException
Username length cannot exceed 255 characters.
Error message
Username length cannot exceed 255 characters.
What it means
Thrown by User.IsUsernameValid(username, throwException: true) when the username is longer than 255 characters (after the null/empty check passes). It is an ArgumentException (parameter Username) enforcing the configured storage width for usernames.
Source
Thrown at DnsServerCore/Auth/User.cs:179
user._ssoIdentifier = ssoIdentifier;
return user;
}
public static bool IsUsernameValid(string username, bool throwException = false)
{
if (string.IsNullOrWhiteSpace(username))
{
if (throwException)
throw new ArgumentException("Username cannot be null or empty.", nameof(Username));
return false;
}
if (username.Length > 255)
{
if (throwException)
throw new ArgumentException("Username length cannot exceed 255 characters.", nameof(Username));
return false;
}
foreach (char c in username)
{
if ((c >= 97) && (c <= 122)) //[a-z]
continue;
if ((c >= 65) && (c <= 90)) //[A-Z]
continue;
if ((c >= 48) && (c <= 57)) //[0-9]
continue;
if (c == '@')
continue;
View on GitHub (pinned to d0484b6c1e)
Solutions
- Shorten the username to <= 255 characters.
- Enforce client-side maxlength and server-side validation on the username field.
- If using email-as-username, truncate or map long addresses to a shorter local identifier.
Example fix
// before
User.IsUsernameValid(username, throwException: true);
// after
if (username?.Length > 255)
return BadRequest("Username length cannot exceed 255 characters."); Defensive patterns
Strategy: validation
Validate before calling
if (username is null || username.Length > 255)
return BadRequest("Username length cannot exceed 255 characters."); Try / catch
try { User.IsUsernameValid(username, throwException: true); }
catch (ArgumentException ex) when (ex.ParamName == "Username" && ex.Message.Contains("255"))
{ return BadRequest(ex.Message); } Prevention
- Set a maxlength on the username input in the UI.
- Validate length server-side before validating characters.
- Map long email/UPN identifiers to shorter local usernames.
When it happens
Trigger: Calling IsUsernameValid(longUsername, true), or a create/rename path that delegates to it, with a username over 255 chars.
Common situations: A UI without a maxlength on the username field; an automated provisioner that builds usernames from long email/UPN values; a migration that did not trim source identifiers.
Related errors
- Group name length cannot exceed 255 characters.
- Username cannot be null or empty.
- Username can contain only alpha numeric, '@', '-', '_', or '
- Display name length cannot exceed 255 characters.
- Token name length cannot exceed 255 characters.
AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13).
Data as JSON: /api/errors/27e46d48fd696a4f.
Report an issue: GitHub.