TechnitiumSoftware/DnsServer · error · ArgumentException

Username can contain only alpha numeric, '@', '-', '_', or '

Error message

Username can contain only alpha numeric, '@', '-', '_', or '.' characters.

What it means

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).

Source

Thrown at DnsServerCore/Auth/User.cs:208

                    continue;

                if ((c >= 48) && (c <= 57)) //[0-9]
                    continue;

                if (c == '@')
                    continue;

                if (c == '-')
                    continue;

                if (c == '_')
                    continue;

                if (c == '.')
                    continue;

                if (throwException)
                    throw new ArgumentException("Username can contain only alpha numeric, '@', '-', '_', or '.' characters.", nameof(Username));

                return false;
            }

            return true;
        }

        #endregion

        #region internal

        internal void SetUsername(string username)
        {
            if (_passwordHashType == UserPasswordHashType.OldScheme)
                throw new InvalidOperationException("Cannot change username when using old password hash scheme. Change password once and try again.");

            IsUsernameValid(username, true);

View on GitHub (pinned to d0484b6c1e)

Solutions

  1. Restrict usernames to [a-zA-Z0-9@-_.] only; strip or reject other characters.
  2. Normalize the input (e.g. take the local part of an email) before validating.
  3. Surface the allowed-character set to the user in the UI so they self-correct.

Example fix

// before
User.IsUsernameValid(username, throwException: true);

// after
static bool AllowedUsername(string s) => !string.IsNullOrEmpty(s)
    && s.All(c => char.IsLetterOrDigit(c) || "@-_./".IndexOf(c) >= 0);
if (!AllowedUsername(username))
    return BadRequest("Username can contain only alpha numeric, '@', '-', '_', or '.' characters.");
Defensive patterns

Strategy: type-guard

Validate before calling

static bool IsValidUsernameChars(string s) =>
    !string.IsNullOrEmpty(s) && s.All(c =>
        (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
        (c >= '0' && c <= '9') || c == '@' || c == '-' || c == '_' || c == '.');

if (!IsValidUsernameChars(username))
    return BadRequest("Username can contain only alpha numeric, '@', '-', '_', or '.' characters.");

Type guard

static bool IsValidUsername(string s) =>
    !string.IsNullOrWhiteSpace(s)
    && s.Length <= 255
    && s.All(c => char.IsLetterOrDigit(c) || "@-_./".IndexOf(c) >= 0);

Try / catch

try { User.IsUsernameValid(username, throwException: true); }
catch (ArgumentException ex) when (ex.ParamName == "Username" && ex.Message.Contains("alpha numeric"))
{ return BadRequest(ex.Message); }

Prevention

When it happens

Trigger: Calling IsUsernameValid(username, true) with a username containing spaces, unicode, or punctuation other than '@','-','_','.' (e.g. parentheses, slashes, colons).

Common situations: 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.

Related errors


AI-assisted analysis of TechnitiumSoftware/DnsServer@d0484b6c1e (2026-08-13). Data as JSON: /api/errors/34e80ac655ef1dda. Report an issue: GitHub.