Register form kentico mvc

web dev asked on May 22, 2020 16:07

Hello i am working on register form and i only have FirstName and email and password fields i want to user (UserName= email)but keep geting error that the user name cant contain digits or symbol like (@ or _ ) here is my code

public async Task<ActionResult> Register(model model)
    {

        if (!ModelState.IsValid)
        {
            return View(model);
        }


        var user = new User
        {

            UserName = model.Email,
            FirstName = model.FirstName,

            Email = model.Email,
            Enabled = true
        };

        var registerResult = new IdentityResult();

        try
        {
            registerResult = await UserManager.CreateAsync(user, model.Password);
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("AccountController", "Register", ex);
            ModelState.AddModelError(String.Empty, ResHelper.GetString("register_failuretext"));
        }

        if (registerResult.Succeeded)
        {




            mMembershipActivitiesLogger.LogRegistration(model.Email);
            await SignInManager.SignInAsync(user, true, false);
            ContactManagementContext.UpdateUserLoginContact(model.Email);

            mMembershipActivitiesLogger.LogLogin(model.Email);

            return RedirectToAction("Index", "Home");
        }



        foreach (var error in registerResult.Errors)
        {
            ModelState.AddModelError(String.Empty, error);
        }


        return View(model);
    }

Recent Answers


Dmitry Bastron answered on May 22, 2020 16:41

Hi,

Have a look at these user name settings in web.config, specifically at CMSEnsureSafeUserNames and CMSUserValidationRegEx as they should be defining this validation logic.

0 votesVote for this answer Mark as a Correct answer

web dev answered on May 22, 2020 18:35

this solution didnt work still get error username email+665@gmail is invalid can only contain

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 22, 2020 18:40

Did you use it in CMS or MVC web.config or both? What line of code produces this error? UserManager.CreateAsync?

0 votesVote for this answer Mark as a Correct answer

web dev answered on May 22, 2020 18:44

i add it web.conf of kentico /cms/web.config

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 22, 2020 19:50

Try doing the same for MVC application because you are running user creation code there, not in CMS.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.