nopSolutions/nopCommerce · warning · NopException

Account.EmailUsernameErrors.UsernameTooLong

Error message

Account.EmailUsernameErrors.UsernameTooLong

What it means

Thrown by SetUsernameAsync when newUsername.Trim().Length exceeds NopCustomerServicesDefaults.CustomerUsernameLength, using localized resource 'Account.EmailUsernameErrors.UsernameTooLong'. Enforces the username column length constraint.

Source

Thrown at src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs:573

    }

    /// <summary>
    /// Sets a customer username
    /// </summary>
    /// <param name="customer">Customer</param>
    /// <param name="newUsername">New Username</param>
    /// <returns>A task that represents the asynchronous operation</returns>
    public virtual async Task SetUsernameAsync(Customer customer, string newUsername)
    {
        ArgumentNullException.ThrowIfNull(customer);

        if (!_customerSettings.UsernamesEnabled)
            throw new NopException("Usernames are disabled");

        newUsername = newUsername.Trim();

        if (newUsername.Length > NopCustomerServicesDefaults.CustomerUsernameLength)
            throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.UsernameTooLong"));

        var user2 = await _customerService.GetCustomerByUsernameAsync(newUsername);
        if (user2 != null && customer.Id != user2.Id)
            throw new NopException(await _localizationService.GetResourceAsync("Account.EmailUsernameErrors.UsernameAlreadyExists"));

        customer.Username = newUsername;
        await _customerService.UpdateCustomerAsync(customer);
    }

    #endregion
}

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Add maxlength on the username input matching NopCustomerServicesDefaults.CustomerUsernameLength.
  2. Validate length before calling SetUsernameAsync.
  3. Trim input before submission.

Example fix

// before
await _customerRegistrationService.SetUsernameAsync(customer, username);

// after
username = username?.Trim();
if (username is { Length: > NopCustomerServicesDefaults.CustomerUsernameLength })
    ModelState.AddModelError(nameof(username), "Username is too long.");
else
    await _customerRegistrationService.SetUsernameAsync(customer, username);
Defensive patterns

Strategy: validation

Validate before calling

var trimmed = newUsername?.Trim();
if (trimmed is { Length: > NopCustomerServicesDefaults.CustomerUsernameLength })
    ModelState.AddModelError(nameof(newUsername), "Username too long.");

Type guard

static bool IsUsernameWithinLimit(string s) => s is null || s.Trim().Length <= NopCustomerServicesDefaults.CustomerUsernameLength;

Try / catch

try { await _customerRegistrationService.SetUsernameAsync(customer, username); }
catch (NopException ex) { ModelState.AddModelError(nameof(username), ex.Message); }

Prevention

When it happens

Trigger: Calling SetUsernameAsync with a username longer than the configured CustomerUsernameLength constant after trimming.

Common situations: User enters a very long username; client-side maxlength missing; test/import data unbounded.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/66c073b5961f3fdb. Report an issue: GitHub.