TechnitiumSoftware/DnsServer · error · ArgumentException
Username cannot be null or empty.
Error message
Username cannot be null or empty.
What it means
Thrown by User.IsUsernameValid(username, throwException: true) when username is null, empty, or whitespace. It is an ArgumentException (parameter Username). IsUsernameValid is the shared validator used by username-setting paths; with throwException it reports the problem instead of returning false.
Source
Thrown at DnsServerCore/Auth/User.cs:171
public static User CreateSsoUser(string displayName, string username, string ssoIdentifier)
{
User user = new User();
user.SetUsername(username);
user.DisplayName = displayName;
user._isSsoUser = true;
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]View on GitHub (pinned to d0484b6c1e)
Solutions
- Supply a non-empty, non-whitespace username.
- Validate presence at the request boundary before calling IsUsernameValid.
- Use IsUsernameValid(username) without throwException to branch cleanly on invalid input.
Example fix
// before
User.IsUsernameValid(username, throwException: true);
// after
if (!User.IsUsernameValid(username))
return BadRequest("Username cannot be null or empty."); Defensive patterns
Strategy: validation
Validate before calling
// Use the non-throwing overload to branch cleanly.
if (!User.IsUsernameValid(username))
return BadRequest("Username cannot be null or empty."); Try / catch
try { User.IsUsernameValid(username, throwException: true); }
catch (ArgumentException ex) when (ex.ParamName == "Username" && ex.Message.Contains("null or empty"))
{ return BadRequest(ex.Message); } Prevention
- Prefer IsUsernameValid without throwException for control flow.
- Require the username field at the API boundary.
- Reject blank usernames during import/migration.
When it happens
Trigger: Calling IsUsernameValid(blank, true) directly, or SetUsername/Create paths that delegate to it with a blank username when throwException is enabled.
Common situations: A create-user form submitted without a username; an import row with a blank username column; an SSO provisioning path that passes a null identifier through to the validator.
Related errors
- Username length cannot exceed 255 characters.
- Username can contain only alpha numeric, '@', '-', '_', or '
- User account is disabled. Please contact your administrator.
- Cannot create more than 255 users.
- User already exists: {username}
AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13).
Data as JSON: /api/errors/9d293fac6ce3ff29.
Report an issue: GitHub.