{"record":{"id":"7ef44c83f6e80682","repo":"fullstackhero/dotnet-starter-kit","slug":"unable-to-register-the-user","errorCode":null,"errorMessage":"Unable to register the user.","messagePattern":"Unable to register the user\\.","errorType":"exception","errorClass":"CustomException","httpStatus":400,"severity":"error","filePath":"src/Modules/Identity/Modules.Identity/Services/UserRegistrationService.cs","lineNumber":267,"sourceCode":"        {\n            Email = email,\n            FirstName = firstName,\n            LastName = lastName,\n            UserName = userName,\n            PhoneNumber = phoneNumber,\n            IsActive = true,\n            EmailConfirmed = false,\n            PhoneNumberConfirmed = false,\n        };\n\n        var result = await userManager.CreateAsync(user, password);\n        if (!result.Succeeded)\n        {\n            // Identity create failures (duplicate email/username, password policy, …) are\n            // client-input errors, not server faults — surface them as 400 with the specific\n            // reasons so the caller sees *why* registration failed, not a bare 500.\n            var errors = result.Errors.Select(error => error.Description).ToList();\n            throw new CustomException(\n                \"Unable to register the user.\",\n                errors,\n                HttpStatusCode.BadRequest);\n        }\n\n        return user;\n    }\n\n    private async Task AssignDefaultRoleAndGroupsAsync(\n        FshUser user,\n        string source,\n        CancellationToken cancellationToken = default)\n    {\n        await userManager.AddToRoleAsync(user, RoleConstants.Basic);\n\n        var defaultGroups = await db.Groups\n            .AsNoTracking()\n            .Where(g => g.IsDefault && !g.IsDeleted)","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/fullstackhero/dotnet-starter-kit/blob/3f2959e683e9f83f13e55e1678c9119f63c7e8e5/src/Modules/Identity/Modules.Identity/Services/UserRegistrationService.cs#L249-L285","documentation":"Thrown by CreateUserWithPasswordAsync when ASP.NET Identity's UserManager.CreateAsync returns a non-succeeded result. It deliberately surfaces Identity's per-property failure descriptions (duplicate email/username, password policy violations, etc.) as a 400 BadRequest instead of a 500, since these are client-input problems.","triggerScenarios":"Calling the user-registration endpoint with an email or username already taken, a password that fails the configured Identity password policy (length, complexity, uniqueness), or any other rule enforced by UserManager (e.g. RequireUniqueEmail).","commonSituations":"Seeding scripts re-registering the same admin twice; frontends not pre-validating password rules that differ from defaults; duplicate signup attempts racing on the same email; environments where password options were tightened after users existed.","solutions":["Read the errorDescriptions array in the response — it lists the exact Identity failure reasons.","Verify the email/username is not already registered before submitting, or use the forgot-password flow for existing accounts.","Bring the password in line with the configured IdentityOptions.Password policy for this environment.","If seeding, make registration idempotent: check for existing user first instead of re-creating."],"exampleFix":"// before: blind create fails on duplicates\nawait registrationService.CreateUserWithPasswordAsync(request, ct);\n// after: pre-check then create\nvar existing = await userManager.FindByEmailAsync(request.Email);\nif (existing is not null) throw new CustomException(\"Email already registered.\", Array.Empty<string>(), HttpStatusCode.BadRequest);\nawait registrationService.CreateUserWithPasswordAsync(request, ct);","handlingStrategy":"validation","validationCode":"const emailRe = /^[^@\\s]+@[^@\\s]+$/;\nif (!emailRe.test(payload.email)) throw new Error(\"Invalid email\");\nif (payload.password.length < 8 || !/[A-Z]/.test(payload.password) || !/\\d/.test(payload.password))\n  throw new Error(\"Password must be 8+ chars with upper case and digit\");","typeGuard":null,"tryCatchPattern":"try { await register(payload); } catch (e) {\n  if (e.status === 400 && Array.isArray(e.errors)) showFieldErrors(e.errors); // per-reason Identity messages\n  else throw e;\n}","preventionTips":["Mirror the server's Identity password policy in client-side validation.","Check email/username availability before final submit.","Make seed scripts idempotent (skip existing users).","Always read errorDescriptions in the 400 body — it names the exact failing rule."],"tags":["aspnet-identity","validation","registration"],"backgroundTag":"schema-validation-failed","analyzedSha":"3f2959e683e9f83f13e55e1678c9119f63c7e8e5","analyzedAt":"2026-09-15T22:20:53.684Z","contentChangedAt":"2026-09-15T22:20:53.684Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}