{"record":{"id":"76f7b2eaffbd8656","repo":"nopSolutions/nopCommerce","slug":"usernames-are-disabled","errorCode":null,"errorMessage":"Usernames are disabled","messagePattern":"Usernames are disabled","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs","lineNumber":568,"sourceCode":"\r\n                subscription.Active = false;\r\n                await _newsLetterSubscriptionService.UpdateNewsLetterSubscriptionAsync(subscription);\r\n            }\r\n        }\r\n    }\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":550,"sourceCodeEnd":584,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Customers/CustomerRegistrationService.cs#L550-L584","documentation":"Thrown by SetUsernameAsync as a NopException when _customerSettings.UsernamesEnabled is false. Username functionality is opt-in via the CustomerSettings.UsernamesEnabled flag; calling SetUsernameAsync while it's disabled is a configuration/caller mismatch.","triggerScenarios":"Invoking SetUsernameAsync (directly or via a 'change username' admin/customer action) when CustomerSettings > UsernamesEnabled is unchecked in admin or set to false in settings.","commonSituations":"A plugin or custom flow assumes usernames are on; an admin toggled UsernamesEnabled off after the feature shipped; default install (usernames often disabled by default).","solutions":["Enable usernames in Admin > Configuration > Settings > Customer settings (UsernamesEnabled = true), or set the setting via code.","Guard the caller: only expose/invoke SetUsernameAsync when _customerSettings.UsernamesEnabled is true.","If usernames must stay off, remove the username-change UI path entirely."],"exampleFix":"// before\nawait _customerRegistrationService.SetUsernameAsync(customer, newUsername);\n\n// after\nif (!_customerSettings.UsernamesEnabled)\n    return; // or notify user that usernames are disabled\nawait _customerRegistrationService.SetUsernameAsync(customer, newUsername);","handlingStrategy":"type-guard","validationCode":"if (!_customerSettings.UsernamesEnabled) return; // usernames feature off\nawait _customerRegistrationService.SetUsernameAsync(customer, newUsername);","typeGuard":"static bool UsernamesAreOn(ICustomerSettings s) => s.UsernamesEnabled;","tryCatchPattern":"try { await _customerRegistrationService.SetUsernameAsync(customer, username); }\ncatch (NopException ex) when (ex.Message == \"Usernames are disabled\")\n{ /* hide the username UI path */ }","preventionTips":["Gate the 'change username' UI behind _customerSettings.UsernamesEnabled.","Enable the setting in Admin > Customer settings before exposing username features.","Document that SetUsernameAsync requires the setting to be on."],"tags":["customer","username","settings","configuration"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}