0

I want to save user phone number when the user registers a form.

My register form is working right now only with 3 fields(EmailID, password, confirm password) using the following code

//SignUp $('#btnSignUp').click(function () { console.log('Signup clicked'); var data = { "Email": $('#txtSingUpEmail').val(), "PhoneNumber": '546789651', //for testing I directly pass the value in JQuery "Password": $('#txtSignUpPassword').val(), "ConfirmPassword": $('#txtConfirmPassword').val() }; $.ajax({ type: 'POST', url: '/ProjectName/api/Account/Register', contentType: 'application/json; charset=utf-8', data: JSON.stringify(data) }).done(function (data) { console.log('Registered Succesfully'); $('#modalSignup').modal('hide'); $('#exampleModalPreview').modal('show'); }).fail(function (showError) { console.log('Signup failed'); console.log('full error = '+JSON.stringify(showError)); //$('#loginErrorMsg').text(JSON.parse(showError.responseText).error_description); }); }); 

AccountController

// POST api/Account/Register [AllowAnonymous] [System.Web.Mvc.ValidateAntiForgeryToken] [Route("Register")] public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } //I add PhoneNumber parameter here var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); } 

I tried by adding PhoneNumber variable on below class, but it never saved with phone numbers

RegisterBindingModel class

public class RegisterBindingModel { [Required] [Display(Name = "Email")] public string Email { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } //[Required] [Display(Name = "PhoneNumber")] public string PhoneNumber { get; set; } } 

UPDATE

I found the following method on metadata

UserManager

[AsyncStateMachine(typeof(UserManager<,>.<SetPhoneNumberAsync>d__d9))] [DebuggerStepThrough] public virtual Task<IdentityResult> SetPhoneNumberAsync(TKey userId, string phoneNumber); 

so when I execute this method on Register method like below code. It enters the phone number where the user is created

// POST api/Account/Register [AllowAnonymous] [System.Web.Mvc.ValidateAntiForgeryToken] [Route("Register")] public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } //I add PhoneNumber parameter here var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); IdentityResult setPhoneNumber = await UserManager.SetPhoneNumberAsync("", model.PhoneNumber); if (!result.Succeeded) { return GetErrorResult(result); } return Ok(); } 

As well as it throws an error that cannot find key. I just take one userId(already registered userID's value) key from table. I pass it on the parameter like below, then it set's the phone number to both user(already registerd user and also newly register)

3
  • Have you checked, that the PhoneNumber property is not null in your action method? Commented Mar 12, 2019 at 20:36
  • @Marco could you please tell me which action method are you talking about?. I am new to this token based authentication. where is it located?. Commented Mar 12, 2019 at 20:43
  • In your Register method, set a breakpoint and check if model.PhoneNumber is null. Commented Mar 13, 2019 at 6:40

1 Answer 1

1

I found the answer

var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); IdentityResult setPhoneNumber = await UserManager.SetPhoneNumberAsync(user.Id, model.PhoneNumber); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.