{"record":{"id":"aaab5340e5806011","repo":"nopSolutions/nopCommerce","slug":"account-emailusernameerrors-usernamealreadyexists","errorCode":null,"errorMessage":"Account.EmailUsernameErrors.UsernameAlreadyExists","messagePattern":"Account\\.EmailUsernameErrors\\.UsernameAlreadyExists","errorType":"validation","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":577,"sourceCode":"    /// </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":559,"sourceCodeEnd":584,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L559-L584","documentation":"Thrown by SetUsernameAsync when GetCustomerByUsernameAsync(newUsername) returns a different customer, using localized resource 'Account.EmailUsernameErrors.UsernameAlreadyExists'. Enforces username uniqueness.","triggerScenarios":"Calling SetUsernameAsync with a username already taken by another customer account.","commonSituations":"User picks a popular handle; admin reassigns a taken username; duplicate-account merge.","solutions":["Pre-check uniqueness with GetCustomerByUsernameAsync before calling SetUsernameAsync.","Suggest available alternatives when a collision is detected.","Surface the localized message to the user as a validation error."],"exampleFix":"// before\nawait _customerRegistrationService.SetUsernameAsync(customer, newUsername);\n\n// after\nvar owner = await _customerService.GetCustomerByUsernameAsync(newUsername);\nif (owner is not null && owner.Id != customer.Id)\n    ModelState.AddModelError(nameof(newUsername), \"This username is already taken.\");\nelse\n    await _customerRegistrationService.SetUsernameAsync(customer, newUsername);","handlingStrategy":"validation","validationCode":"var owner = await _customerService.GetCustomerByUsernameAsync(newUsername);\nif (owner is not null && owner.Id != customer.Id)\n    ModelState.AddModelError(nameof(newUsername), \"Username already taken.\");","typeGuard":null,"tryCatchPattern":"try { await _customerRegistrationService.SetUsernameAsync(customer, username); }\ncatch (NopException ex) { ModelState.AddModelError(nameof(username), ex.Message); }","preventionTips":["Pre-check username uniqueness before submission.","Offer alternative suggestions on collision.","Surface the localized message as a validation error."],"tags":["customer","username","duplicate","localization","validation"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}