{"record":{"id":"66c073b5961f3fdb","repo":"nopSolutions/nopCommerce","slug":"account-emailusernameerrors-usernametoolong","errorCode":null,"errorMessage":"Account.EmailUsernameErrors.UsernameTooLong","messagePattern":"Account\\.EmailUsernameErrors\\.UsernameTooLong","errorType":"validation","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":573,"sourceCode":"    }\r\n\r\n    /// <summary>\r\n    /// Sets a customer username\r\n    /// </summary>\r\n    /// <param name=\"customer\">Customer</param>\r\n    /// <param name=\"newUsername\">New Username</param>\r\n    /// <returns>A task that represents the asynchronous operation</returns>\r\n    public virtual async Task SetUsernameAsync(Customer customer, string newUsername)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(customer);\r\n\r\n        if (!_customerSettings.UsernamesEnabled)\r\n            throw new NopException(\"Usernames are disabled\");\r\n\r\n        newUsername = newUsername.Trim();\r\n\r\n        if (newUsername.Length > NopCustomerServicesDefaults.CustomerUsernameLength)\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.UsernameTooLong\"));\r\n\r\n        var user2 = await _customerService.GetCustomerByUsernameAsync(newUsername);\r\n        if (user2 != null && customer.Id != user2.Id)\r\n            throw new NopException(await _localizationService.GetResourceAsync(\"Account.EmailUsernameErrors.UsernameAlreadyExists\"));\r\n\r\n        customer.Username = newUsername;\r\n        await _customerService.UpdateCustomerAsync(customer);\r\n    }\r\n\r\n    #endregion\r\n}","sourceCodeStart":555,"sourceCodeEnd":584,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L555-L584","documentation":"Thrown by SetUsernameAsync when newUsername.Trim().Length exceeds NopCustomerServicesDefaults.CustomerUsernameLength, using localized resource 'Account.EmailUsernameErrors.UsernameTooLong'. Enforces the username column length constraint.","triggerScenarios":"Calling SetUsernameAsync with a username longer than the configured CustomerUsernameLength constant after trimming.","commonSituations":"User enters a very long username; client-side maxlength missing; test/import data unbounded.","solutions":["Add maxlength on the username input matching NopCustomerServicesDefaults.CustomerUsernameLength.","Validate length before calling SetUsernameAsync.","Trim input before submission."],"exampleFix":"// before\nawait _customerRegistrationService.SetUsernameAsync(customer, username);\n\n// after\nusername = username?.Trim();\nif (username is { Length: > NopCustomerServicesDefaults.CustomerUsernameLength })\n    ModelState.AddModelError(nameof(username), \"Username is too long.\");\nelse\n    await _customerRegistrationService.SetUsernameAsync(customer, username);","handlingStrategy":"validation","validationCode":"var trimmed = newUsername?.Trim();\nif (trimmed is { Length: > NopCustomerServicesDefaults.CustomerUsernameLength })\n    ModelState.AddModelError(nameof(newUsername), \"Username too long.\");","typeGuard":"static bool IsUsernameWithinLimit(string s) => s is null || s.Trim().Length <= NopCustomerServicesDefaults.CustomerUsernameLength;","tryCatchPattern":"try { await _customerRegistrationService.SetUsernameAsync(customer, username); }\ncatch (NopException ex) { ModelState.AddModelError(nameof(username), ex.Message); }","preventionTips":["Bind the username input maxlength to NopCustomerServicesDefaults.CustomerUsernameLength.","Trim input before validation.","Validate length server-side too."],"tags":["customer","username","validation","localization"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}